使用AJAX的表单复选框始终显示使用php

时间:2013-04-10 05:44:40

标签: php jquery ajax

我有一个复选框

<input name="notify" type="checkbox" id="notify" class="notify" />

我正在使用ajax调用

$("form#submit").submit(function() {
// we want to store the values from the form input box, then send via ajax below
var username = $('#username').attr('value');
var email = $('#email').attr('value');
var password = $('#password').attr('value');
var cpassword = $('#cpassword').attr('value');
var notify = $('#notify').attr('value');
var session_user_id = $('#session_user_id').attr('value');
$.ajax({
type: "POST",
url: "../../includes/editprofile.php?",
data: "username="+ username+
    "&email="+ email+
    "&password="+ password+
    "&cpassword="+ cpassword+
    "&notify="+ notify+
    "&session_user_id="+ session_user_id,
success: function(data){
$('div.success').fadeIn();
$('div.success').html(data);
}
});
return false;
});

在我的editprofile.php文件中,我只是echo'n $ notify,看看我提交表单后的值是什么,无论是否总是“打开”我是否选中此框。有线索吗?

3 个答案:

答案 0 :(得分:3)

所有3应该在

下面工作

尝试

$('#notify').is(':checked');

$('#notify').attr('checked');

$('#notify').prop('checked'); 

答案 1 :(得分:2)

更改此

var notify = $('#notify').attr('value');

var notify = $('#notify').attr('checked');

因为checkbox没有value

答案 2 :(得分:0)

试试这个

$('#notify').attr('checked') ? "True" : "False";