2012-04-04 62 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