2011-08-13 59 views
1

我有以下的html代碼:jquery - 如何選擇具有此屬性的所有輸入?

<input data-autocomplete="/items/autocomplete_part_title?locale=en" data-id-element="#autocomplete_38219" id="item_item_parts_attributes_0_autocomplete_part_id" name="item[item_parts_attributes][0][autocomplete_part_id]" size="30" type="text" /> 
<input data-autocomplete="/items/autocomplete_part_title?locale=en" data-id-element="#autocomplete_5791" id="item_item_parts_attributes_0_autocomplete_part_id" name="item[item_parts_attributes][0][autocomplete_part_id]" size="30" type="text" /> 

我能有這樣的一個或多個輸入字段。

,我想知道我怎麼能觸發一個事件時,這個領域失去焦點(模糊)

我想選擇所有的「數據ID元素」開始「#autocomplete_」

$("input#data-id-element^=autocomplete").each().live("blur", function() { 
alert('a'); 
}); 

這樣的事情,任何方式?

回答

1

嘗試這種情況:

$("input[data-id-element^='#autocomplete']").live("blur", function() { 
alert('a'); 
}); 

工作實施例:http://jsfiddle.net/V7N5w/

+0

這個工程,但只達到第一個元素,但謝謝! –

+0

解決了關於循環的事情;) –

+0

代碼中存在拼寫錯誤。修復。再試一次.. – Chandu

1

找到我可以救援人員到場所有元素執行一個循環匹配這樣做:

$("input[data-id-element^='#autocomplete']").live("blur", function(p) { 
    p.preventDefault(); 
    alert('a'); 
}); 

代替使用各()方法的。

相關問題