Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 复选框总是返回false_Javascript_Vb.net_Asp.net Mvc 3_Razor - Fatal编程技术网

Javascript 复选框总是返回false

Javascript 复选框总是返回false,javascript,vb.net,asp.net-mvc-3,razor,Javascript,Vb.net,Asp.net Mvc 3,Razor,我读过一些关于复选框总是返回错误状态的文章,但没有发现关于我自己问题的任何东西 下面是脚本: <script type="text/javascript"> function update_contact(id, name) { alert("idCont : " + id + "\n nameCKB : " + name + "\n state : " + $(this).attr('checked')); v

我读过一些关于复选框总是返回错误状态的文章,但没有发现关于我自己问题的任何东西

下面是脚本:

<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>

功能更新\u联系人(id、姓名){
警报(“idCont:+id+”\n nameCKB:+name+“\n状态:”
+$(this.attr('checked');
var a=location.pathname.substring(1.split(“/”)
$.ajax({
url:'@url.Action(“更新联系人”),
键入:“POST”,
数据:{
姓名:姓名,,
isChecked:$(this).is(“:checked”),
idOpp:a[2],
idCont:id
},
成功:功能(结果){
}
});
};
下面是复选框的代码:

@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
@如果mail=False,则
@
其他的
@
如果结束
下面是服务器生成的代码:

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

起初,我使用了一个Html助手。它是这样返回smth的:

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

我认为这是由于第二次输入,它总是返回错误状态。

试试这个(这里的工作小提琴:):


功能更新\u联系人(id、姓名){
变量源=$(“#”+名称);
警报(“idCont:+id+”\n nameCKB:+name+”\n状态:“+source.attr('checked'));
var a=location.pathname.substring(1.split(“/”)
$.ajax({
url:'@url.Action(“更新联系人”),
键入:“POST”,
数据:{
姓名:姓名,,
isChecked:source.is(“:checked”),
idOpp:a[2],
idCont:id
},
成功:函数(结果){}
});
};

问题很可能是
这个
不是你想象的那样。尝试将对已单击复选框的引用显式传递给函数:

@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
或者,使用jQuery分配click处理程序,它将为您正确设置
。您可以将
@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

(注意:我实际上不知道VB/razor语法,我只是猜测一下。)

Mamoo是正确的:$。ajax是一个jQuery对象,因此
$(此)
不会指向调用update\u contact函数的元素。我不会像mamoo那样创建两个变量(
source
ans
a
),而是在
$之前创建
var数据

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)
正常工作,而不是传递name和id参数-这意味着您在某处选择了checkbox元素,并且您正在以类似以下方式调用函数:

update_contact($(elem).attr('id'),$(elem).attr('name'));
只需使用更短、更干净、更强大的:

update_contact.call($(elem));

或者将内联的
onclick=“update\u contact()”
更改为
onclick=“update\u contact.call(this)”
。仅此而已,没有$符号或括号

这不会有任何区别,因为它仍然在
update\u contact()
函数的作用域中使用
this
。我不确定它是否会工作,因为在我的警报中,我也总是有一个“false”属性。是否将
update\u contact
用作事件处理程序?我以为是。。。如果不是,则NNN是正确的。可以通过显式调用此函数来修复此问题,如so
update_contact.call($('#id')).call
方法或使用
.bind('event',callback)
方法。该方法工作得非常好!谢谢大家。这是我选择的解决方案,因为我能够理解它。@EliasVanOotegem-the
$.ajax()
部分与此无关。在回答的前半部分,我从内联处理程序传递
这个
,因此
cb
参数将是复选框。我在回答的后半部分中给出的另一种方法是使用jQuery创建单击处理程序,这样jQuery将
这个
设置为单击的元素。aw。。。波比科克,你应该仔细看看你的答案。。。很抱歉
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) {}
        });
}
update_contact($(elem).attr('id'),$(elem).attr('name'));
update_contact.call($(elem));