2009-09-04 44 views
1

我有一個下拉框中的每一行表JQuery的選擇元素在標準

<table> 
<tr> 
<th> 
<select name="priorityID" id="priorityID"> 
<option label="Important" value="1" selected="selected">Important</option> 
<option label="semi important" value="2">semi important</option> 
<option label="normal" value="3">normal</option> 
<option label="not important" value="4">not important</option> 
</select> 

</th> 

</tr> 
<tr> 
<th> 
<select name="priorityID" id="priorityID"> 
<option label="Important" value="1" selected="selected">Important</option> 
<option label="semi important" value="2">semi important</option> 
<option label="normal" value="3">normal</option> 
<option label="not important" value="4">not important</option> 
</select> 

</th> 

裏面的問題是現在每當priorityID變化,我需要調用一個jquery功能更新數據庫。現在,由於每行都有自己的下拉框,因此如何編寫JQuery以便在JQuery端可以捕獲哪個行的下拉框來觸發事件?

回答

2

你可以把每個選擇一個ID,並把它作爲當您使用jQuery.post/get的數據值。

然後,您可以在服務器站點獲取它,以確定選擇框發生了什麼變化。

<select name="priorityID" id="1"> 
<select name="priorityID" id="2"> 
<select name="priorityID" id="3"> 

$("select").change(function() { 
    $.post(

    "http://api.url.com/method", 

    { 
     "selectId"  : $(this).attr('id'), 
     "selectedValue" : $(this).val() 
    }, 

    function(data) { 
     alert('Back!'); 
    } 

); 
}); 
2

你可以用一些radness一起使用change事件:

$("select").change(function() { 
    var offset = $(this).closest("tr").prevAll().length; 
    alert(offset); 
});