這是我想實現的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;
});
});
你爲什麼用'.data()'來存儲兄弟姐妹,而不是在你準備好使用它們時選擇它們?你爲什麼重新jQuery化他們在'keyup'回調? – 2013-03-04 18:43:38
爲什麼要綁定DOMready函數之外的'keyup'事件? – Bergi 2013-03-04 18:45:16
@Bergi可能是因爲所有這些代碼都是在小提琴的窗口加載中運行的。這不是一個藉口... – 2013-03-04 18:48:22