2014-09-26 205 views
2

假設你有下面的代碼更新許多元素:如何在多個元素上處理相同的jQuery事件處理程序?

<div class='item'> 
    Name:<input type='text' name='name' data-id='123' value='Name 1'> 
    <button class='update'>update</button> 
</div> 

<div class='item'> 
    Name:<input type='text' name='name' data-id='456' value='Name 2'> 
    <button class='update'>update</button> 
</div> 

和JavaScript來處理更新:

function updateName(name, id){ 
    ... 
} 

什麼是jQuery的處理程序,可以找到價值和ID和呼叫updateName?

$('button.update').click(function() { 
    name = ? 
    id = ? 
    updateName(name, id) 
} 

編輯:

預期的行爲是,當用戶更新輸入字段,更新後的值發送到updateName(),而不是原始值。

此外,請注意data-id和value是在輸入上,而不是在按鈕上。

http://jsfiddle.net/e3g6nfc1/8/

回答

2

像這樣:

$('button.update').click(function() { 
    name = $(this).attr('name');//to get value of name attribute 
    id = $(this).data('id'); // to get value of attribute data-id 
    updateName(name, id) 
} 

您也可以使用道具()方法得到name or data-id

name = $(this).prop('name'); 
id = $(this).prop('data-id'); 

但最好是使用數據()方法data-*屬性。

+0

我相信'prop'是name屬性 – 2014-09-26 21:46:00

+0

是的,你說得對... – 2014-09-26 21:46:51

+0

它是如何從輸入獲取名稱和ID時,處理程序是首選API附在按鈕上? – 2014-09-26 23:28:37

4

內部事件處理this是要在這種情況下var否則nameid跳出該函數的範圍,並可能成爲全局變量展開的DOM元素

$('button.update').click(function() { 
    var name = this.name 
    var id = $(this).data('id'); 
    updateName(name, id) 
} 

音符。

I've argued before that this is awkward and bad to use。你可以達到相同的效果,而不需要像這樣使用它:

$('button.update').click(function(e) { 
    var name = e.currentTarget.name 
    var id = $(e.currentTarget).data('id'); 
    updateName(name, id) 
} 

你也不必使用jquery的數據。在reasonably modern browsers$(domElement).data('foo')相當於domElement.dataset['foo']


編輯:不知道如果我錯過了這問題,或者如果它得到了編輯,但好像你問不爲元素上的按鈕屬性,但在它之前。在這種情況下,你想$.fn.prev這將是這個樣子

$('button.update').click(function(e) { 
    var $prev = $(e.currentTarget).prev(); 
    updateName($prev.name, $prev.data('id')) 
} 

注意,這個假設輸入元件直接按鈕之前。如果你想找到最接近的前輸入元素你會使用$.fn.prevAll

var $prev = $(e.currentTarget).prevAll('input').last(); 
+0

我沒有得到你的鏈接'這是尷尬和壞'使用'在這裏在你的答案... – 2014-09-26 21:49:35

+0

@ C-linkNepal Huh?基本上我的觀點是'this'是javascript的一個不好的部分。它和其他參數一樣(如果你只使用'.call()'來調用函數,它就變得非常明顯)。唯一的區別是1)你沒有得到它的名字,2)如果你使用簡寫的'fn()'調用語法,javascript只是猜測它應該是什麼。它幾乎總是更好地使用常規參數。 – 2014-09-26 21:54:46

+0

在這個答案中,我還沒有成爲這個。 (我不認爲它會出現在你的回答中,它不是關於'this') – 2014-09-26 21:57:02

1

不知道爲什麼這裏的答案是試圖找到button元素的input屬性...

<div id="container"> 
    <div class='item'> 
     Name:<input type='text' name='name' data-id='123' value='Name 1'> 
     <button class='update'>update</button> 
    </div> 

    <div class='item'> 
     Name:<input type='text' name='name' data-id='456' value='Name 2'> 
     <button class='update'>update</button> 
    </div> 
</div> 

下放事件僅附加到容器(僅選擇與.update類是兒童),抓住他們,因爲他們「冒泡」。 previousElementSibling用於定位input,但如果佈局更復雜,也可以使用jQuery選擇器來查找它。

$('#container').on('click','.update',function(e) { 
    updateName(
     e.currentTarget.previousElementSibling.name, 
     $(e.currentTarget.previousElementSibling).data('id') 
    ); 
}); 

JSFiddle

+0

在JS小提琴中,輸入一個新名字後,我仍然得到HTML中設置的值。我期待得到更新的價值。 – 2014-09-26 23:34:04

+0

@BSeven'updateName(name,id)'「name」實際上是「value」?或ID? [JSFiddle更新](http://jsfiddle.net/MrYellow/e3g6nfc1/33/) – MrYellow 2014-09-27 02:21:36

+0

你是否在使用HTML屬性'name ='name''做任何事情?如果不是,通常的設置是從'name'屬性中的'data-id'獲得東西。名稱=值。 – MrYellow 2014-09-27 02:25:22

相關問題