Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.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 复选框onChange-won';t火_Javascript - Fatal编程技术网

Javascript 复选框onChange-won';t火

Javascript 复选框onChange-won';t火,javascript,Javascript,我使用一个.js文件包含了下面的脚本,它不会启动。我对JQuery非常陌生,看不出哪里出了问题 请帮忙 我现在已将脚本更改为以下内容,并进行了以下更改: 使用Theophilus的示例代码修改了我的代码,现在它可以正确启动了,谢谢 Julian$(this).attr(':checked')不适用于我,因为它返回“unassigned”。但是使用.prop可以$(this).prop('checked')按预期工作(true/false)谢谢 Rashet,出于测试目的,我将url更改为“/my

我使用一个.js文件包含了下面的脚本,它不会启动。我对JQuery非常陌生,看不出哪里出了问题

请帮忙

我现在已将脚本更改为以下内容,并进行了以下更改: 使用Theophilus的示例代码修改了我的代码,现在它可以正确启动了,谢谢 Julian$(this).attr(':checked')不适用于我,因为它返回“unassigned”。但是使用.prop可以$(this).prop('checked')按预期工作(true/false)谢谢 Rashet,出于测试目的,我将url更改为“/my.script.php”,其中包含一个简单的测试脚本,但仍然不起作用

if(isset($_POST['id']) || isset($_POST['Purchased'])){
die('Eureka! variables exist');
}else{
die('Failure!, variables do not exist');
}

$('input[name=Purchased]').change(function(){
    //if a checkbox with name 'Purchased' is clicked, do the following.
    var id=$(this).attr('id');  //grab the id from the clicked box
    var Purchased=$(this).prop('checked');

    //alert("ID value:"+id); returns correctly
//  alert("Purchase value:"+Purchased); //Returns correctly

    //setup the ajax call
//Not posting the variable values to PHP processing script
    $.ajax({
        type: 'POST',
        url: '/my.script.php',
        data: {id: 'id',
        Purchased: 'Purchased'}
    });
});
$(this).val()
应该是
$(this).prop('checked')
,它将返回
true
false

这应该是
.is(选择器)
这篇文章的帮助,您可以在这里阅读 运行下面的代码段以查看它的运行情况

$('input[name=Purchased]')。更改(函数(){
如果(!$(this).is(“:checked”)){
//未检查
警报(“未检查”);
返回;
}
警报(“已选中继续”);
//检查这里
//如果单击了名为“已购买”的复选框,请执行以下操作。
var id=$(this.attr('id');//从单击的框中获取id
var Purchased=$(this).val();//从复选框中获取值(请记住,如果选中该复选框,则该值将为“开”,否则为“开”
警报(“购买价值:+已购买”);
//设置ajax调用
$.ajax({
键入:“POST”,
url:“../Includes/MyPHP.script.php”,
数据:{id:'id',
已购买:'已购买'}
});
});


URL不正确。您不能引用本地文件系统,也不能引用当前位置以上的任何内容。如果您的代码同时包含php和javascript,请添加php标记。谢谢Theophilus,示例代码使其更加清晰。Owk谢谢,如果有帮助,您可以将其标记为您的答案。谢谢Jim,我发现$(this).attr(':checked')不起作用,但是$(this).prop('checked')起作用。感谢您的帮助。