我讀過一些關於複選框的文章,總是返回一個錯誤的狀態,但沒有發現任何關於我自己的問題。複選框allways returned false
所以,這裏是腳本:
<script type="text/javascript">
function update_contact(id, name) {
alert("idCont : " + id + "\n nameCKB : " + name + "\n state : "
+ $(this).attr('checked'));
var a = location.pathname.substring(1).split('/')
$.ajax({
url : '@Url.Action("update_contact")',
type : 'POST',
data : {
name : name,
isChecked : $(this).is(':checked'),
idOpp : a[2],
idCont : id
},
success : function(result) {
}
});
};
</script>
這裏是複選框代碼:
@If mail = False Then
@<input type="checkbox" name="mailed" id="mailed" class="mailed" onclick="update_contact(@item.idContact.toString() , 'mailed')" />
Else
@<input type="checkbox" name="mailed" id="mailed" class="mailed" onclick="update_contact(@item.idContact.toString() , 'mailed')" checked="checked" />
End If
,這裏是由服務器生成的代碼:
<input name="mailed" id="mailed" class="mailed" onclick="update_contact(1 , 'mailed')" type="checkbox">
在一開始,我使用了一個Html幫手。它是這樣返回:
<input id="mailed" name="mailed" onclick="update_contact(1 ,'mailed')" value="true" type="checkbox">
<input name="mailed" value="false" type="hidden">
我雖然是由於第二個輸入,它總是返回一個錯誤的狀態。
(抱歉,如果我的英語不好,我不是以英語爲母語)
它確實工作得很好! thx很多人。這是我選擇的解決方案,因爲我能夠理解它。 – 2012-04-04 10:15:02
@EliasVanOotegem - '$ .ajax()'部分與它無關。在我的答案的前半部分,我從內聯處理函數傳遞'this',所以'cb'參數將成爲複選框。我在答案的後半部分給出的替代方案使用jQuery創建點擊處理程序,以便jQuery將「this」設置爲單擊的元素。 – nnnnnn 2012-04-04 10:43:12
aw ... poppycock,應該仔細看看你的答案......對不起 – 2012-04-04 11:01:13