复选框传真返回f​​alse [英] checkbox allways returning false

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

问题描述

我读过几篇文章谈论复选框始终返回一个错误的状态,但还没有找到我自己的问题,任何东西。

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>

这里是复选框的code:

And here is the checkbox's code :

@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

这是由服务器生成的code:

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.

(对不起,如果我的英语不好,我不是以英语为母语)

(sorry if my english is bad, I'm not a native english speaker)

推荐答案

这个问题可能是这个是不是你认为它是。尽量明确地传递一个参考点击复选框以你的函数:

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分配单击处理程序,它会正确设置这个为您服务。你可以把 @ item.idContact.toString()属性,然后使用访问 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.)

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

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