2013-03-04 162 views
2

這是我想實現的working demo。只要在輸入中輸入一些值,你就可以得到我想要達到的。 (是的,我得到它的工作,但留在..)
但它失敗時,多個鍵被按在一起。將jQuery對象傳遞給函數

我在嘗試:
我有屏幕,其中包含幾個啓用和很少禁用的輸入元素。無論何時用戶更新可編輯輸入元素中的任何值,我想更新具有與用戶更新值相同的值的禁用輸入。

HTML:

<input value="foo" /> // When User updates this 
<br/> 
<input value="bar"> 
<br/> 
<input value="Hello"> 
<br/> 
<input value="World"> 
<br/> 
<input value="foo" disabled> // this should be updated 
<br/> 
<input value="bar" disabled> 
<br/> 
<input value="foo" disabled> // and this also 
<br/> 
<input value="bar" disabled> 
<br/> 
<input value="Happy Ending!"> 
<br/> 

我嘗試這樣做,我認爲會救我脫離multiple_clicks_at_a_time
JS:

$(":input:not(:disabled)").keyup(function() { 
    // Get user entered value 
    var val = this.value; 

    // Find associated inputs which should be updated with new value 
    siblings = $(this).data("siblings"); 
    $(siblings).each(function() { 
     // Update each input with new value 
     this.value = val; 
    }); 
}); 

$(function() { 
    $(":input:not(:disabled)").each(function() { 
     // Find inputs which should be updated with this change in this input 
     siblings = $(":input:disabled[value=" + this.value + "]"); 

     // add them to data attribute 
     $(this).data("siblings", siblings); 
    }); 
}); 

但我不能夠選擇器傳遞給keyup功能和調用.each在上面。


PS:

我以前完全不同的嘗試,與single_click_at_a_time工作,但我覺得我不必再又一次跨越DOM便放棄了這個

$(":input").keypress(function() { 
    $(this).data("oldVal", this.value); 
}); 

$(":input").keyup(function() { 
    var oldVal = $(this).data("oldVal"); 
    $(this).data("newVal", this.value); 
    var newVal = $(this).data("newVal"); 

    $("input:disabled").each(function() { 
     if (this.value == oldVal) this.value = newVal; 
    }); 
}); 
+2

你爲什麼用'.data()'來存儲兄弟姐妹,而不是在你準備好使用它們時選擇它們?你爲什麼重新jQuery化他們在'keyup'回調? – 2013-03-04 18:43:38

+3

爲什麼要綁定DOMready函數之外的'keyup'事件? – Bergi 2013-03-04 18:45:16

+0

@Bergi可能是因爲所有這些代碼都是在小提琴的窗口加載中運行的。這不是一個藉口... – 2013-03-04 18:48:22

回答

1
// Find associated inputs which should be updated with new value 
siblings = $(this).data("siblings", siblings); 

不。用兩個參數調用的.data method沒有得到,但設置了數據(並返回當前選擇)。此外,你應該讓你的局部變量:

var siblings = $(this).data("siblings"); 

Working demo

+0

糟糕,我的複製粘貼錯誤。不工作也是http://jsfiddle.net/Ajinkya_Parakh/fjtFA/10/。仍然+1指出它 – xyz 2013-03-04 18:55:48

+0

不能我們存儲jQuery對象(可能在數據中),並檢索它們,並調用任何函數就像'.each'? – xyz 2013-03-04 19:02:18

+0

你可以。看到我的工作演示。在你的版本10小提琴中,你以某種方式將'兄弟姐妹'值存儲在'兄弟姐妹'的數據屬性中。 – Bergi 2013-03-04 19:05:32

2

我會組這些輸入第一和結合已啓用元素應用到該組的處理程序。見下文,

var grpInp = {}; 

$(":input").each(function() { 
    if (grpInp.hasOwnProperty(this.value)) { 
     grpInp[this.value] = grpInp[this.value].add($(this)); 
    } else { 
     grpInp[this.value] = $(this); 
    } 
}); 

$.each(grpInp, function (i, el) {  
    el.filter(':enabled').keyup(function() { 
     el.val(this.value); 
    }); 
}); 

DEMO:具有相同的值http://jsfiddle.net/fjtFA/9/

上述方法基本上組輸入元件,然後將它們基於:enabled濾波器並結合一個處理程序,以將其應用到該組。

+0

哦gosh.Short和甜,但我只是不明白它。如果你可以添加一些光,它會很棒 – xyz 2013-03-04 18:57:46

+0

通過'value'對所有輸入元素進行分組,然後第一個'.each' grpInp '對象看起來像'{foo:[input1,input4,input6],...}',其中'input1'是第一個值爲'foo'的元素,'input4'是第四個輸入元素,其值爲'foo所以基本上它將輸入按值分組,第二次迭代用於按組迭代,並將鍵控處理程序綁定到組中的** enabled **元素。處理程序只是將值更新爲組中的所有元素。 – 2013-03-04 19:03:08

+0

Just Awesome。:) – xyz 2013-03-04 19:09:15