2012-04-04 25 views
1

我讀過一些關於複選框的文章,總是返回一個錯誤的狀態,但沒有發現任何關於我自己的問題。複選框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"> 

我雖然是由於第二個輸入,它總是返回一個錯誤的狀態。

(抱歉,如果我的英語不好,我不是以英語爲母語)

回答

1

的問題很可能是this不你認爲它是什麼。儘量明確地傳遞一個參考點擊複選框以你的函數:

@If mail = False Then 
     @<input type="checkbox" name="mailed" id="mailed" class="mailed" onclick="update_contact(@item.idContact.toString() , 'mailed', this)" /> 
Else 
     @<input type="checkbox" name="mailed" id="mailed" class="mailed" onclick="update_contact(@item.idContact.toString() , 'mailed', this)" checked="checked" /> 
End If 

然後:

function update_contact(id, name, cb) { 
     alert("idCont: " + id + "\n nameCKB: " + name + "\n state: " + cb.checked); 
     // you could say $(cb).attr("checked"), but cb.checked is more efficient 
     // and easier to read 
     var a = location.pathname.substring(1).split('/') 
     $.ajax({ 
      url: '@Url.Action("update_contact")', 
      type: 'POST', 
      data: { 
        name: name, 
        isChecked: cb.checked, 
        idOpp: a[2], 
        idCont: id 
      }, 
      success: function (result) {} 
     }); 
    }; 

或者使用jQuery分配click處理程序,它會正確設置this你。你可以把@item.idContact.toString()value屬性,然後在你的處理器使用this.value訪問:

@If mail = False Then 
     @<input type="checkbox" name="mailed" id="mailed" class="mailed" value="@item.idContact.toString()" /> 
Else 
     @<input type="checkbox" name="mailed" id="mailed" class="mailed" value="@item.idContact.toString()" checked="checked" /> 
End If 

然後:

$(document).ready(function() { 
    $("#mailed").click(function() { 
     alert("idCont: " + this.value + "\n nameCKB: " + this.name + "\n state: " + this.checked); 
     // you could say $(this).attr("checked"), but this.checked is more efficient 
     // and easier to read 
     var a = location.pathname.substring(1).split('/') 
     $.ajax({ 
      url: '@Url.Action("update_contact")', 
      type: 'POST', 
      data: { 
        name: this.name, 
        isChecked: this.checked, 
        idOpp: a[2], 
        idCont: this.value 
      }, 
      success: function (result) {} 
     }); 
    }); 
}); 

(注:我真的不知道VB /剃刀語法,我只是猜測那部分。)

+0

它確實工作得很好! thx很多人。這是我選擇的解決方案,因爲我能夠理解它。 – 2012-04-04 10:15:02

+1

@EliasVanOotegem - '$ .ajax()'部分與它無關。在我的答案的前半部分,我從內聯處理函數傳遞'this',所以'cb'參數將成爲複選框。我在答案的後半部分給出的替代方案使用jQuery創建點擊處理程序,以便jQuery將「this」設置爲單擊的元素。 – nnnnnn 2012-04-04 10:43:12

+0

aw ... poppycock,應該仔細看看你的答案......對不起 – 2012-04-04 11:01:13

1

(在這裏工作的小提琴:http://jsfiddle.net/Ac3kd/):試試這個

<script type="text/javascript"> 
    function update_contact(id, name) { 
     var source = $('#'+name); 
     alert("idCont : " + id + "\n nameCKB : " + name + "\n state : " + source.attr('checked')); 
     var a = location.pathname.substring(1).split('/') 
     $.ajax({ 
      url: '@Url.Action("update_contact")', 
      type: 'POST', 
      data: { 
        name: name, 
        isChecked: source.is(':checked'), 
        idOpp: a[2], 
        idCont: id 
      }, 
      success: function (result) {} 
     }); 
    }; 
</script> 
1

Mamoo是正確的:$ .ajax是一個jQuery對象,因此$(this)不會指向調用update_contact函數的元素。而不是創建兩個變量(source ANS a)作爲mamoo做,我創建的var data只是$.ajax位之前:

function update_contact(id,name) 
{ 
    var data = {name: name, 
       isChecked: $(this).is(':checked'), 
       idOpp: location.pathname.substring(1).split('/')[2], 
       idCont: id}; 
    $.ajax({ 
      url: '@Url.Action("update_contact")', 
      type: 'POST', 
      data: data, 
      success: function (result) {} 
     }); 
} 

編輯

$(this)工作,而不是通過名稱, id參數 - 這意味着您在某處選擇了複選框元素,並且您以類似於此的方式調用該函數:

update_contact($(elem).attr('id'),$(elem).attr('name')); 

只是用更短,更清潔,更強大:

update_contact.call($(elem)); 

或更改在線onclick="update_contact()"onclick="update_contact.call(this)"。只是這個,沒有$符號或括號...

+0

這不會有什麼區別,因爲它仍然在'update_contact()'函數的作用域中使用'this'。 – nnnnnn 2012-04-04 09:55:39

+0

我不知道它會工作,因爲在我的戒備中,我也總是有一個「虛假」的財產。 – 2012-04-04 09:56:26

+0

不是用作事件處理程序的'update_contact'嗎?我認爲這是...如果沒有,nnnnn是正確的。這可以通過像'update_contact.call($('#id'));'顯式調用這個函數來解決,或者通過綁定函數來固定。 @patxy:如果prop始終爲false,那麼函數可能不會綁定到元素,請使用顯式的'.call'方法或使用'.bind('event',callback)'方法 – 2012-04-04 10:00:31

相關問題