复选框始终返回false [英] checkbox always returning false

查看:174
本文介绍了复选框始终返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读过几篇有关复选框总是返回错误状态的文章,但是对于我自己的问题却一无所获.

I've read a few articles talking about checkboxes always returning a false state, but haven't found anything about my own problem.

所以,这是脚本:

<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

这是服务器生成的代码:

And here is the code generated by the server :

<input name="mailed" id="mailed" class="mailed" onclick="update_contact(1 , 'mailed')" type="checkbox">

开始时,我使用了HTML帮助程序.它正在返回这样的东西:

In the beginning, I used a Html helper. It was returning smth like that:

<input id="mailed" name="mailed" onclick="update_contact(1 ,'mailed')" value="true" type="checkbox">
<input name="mailed" value="false" type="hidden">

尽管我是由于第二次输入而总是返回错误状态.

I though it was due to the second input that it was always returning a false state.

推荐答案

问题很可能是this不是您想的那样.尝试将对所单击复选框的引用显式传递给函数:

The problem is likely that this is not what you think it is. Try explicitly passing a reference to the clicked checkbox to your function:

@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分配点击处理程序,它将为您正确设置this.您可以将@item.idContact.toString()放在value属性中,然后在处理程序中使用this.value对其进行访问:

Alternatively use jQuery to assign the click handler and it will set this for you correctly. You could put the @item.idContact.toString() in the value attribute and then access it using this.value in your handler:

@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/剃刀的语法,我只是在猜测这一部分.)

(Note: I don't actually know the VB/razor syntax, I'm just guessing on that part.)

这篇关于复选框始终返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆