我正在尝试做一些简单的事情,以便在检查表单中的复选框时在数据库上触发查询,但是当用户单击它时,简单就无能为力。我尝试过 onClick 或 onChange 事件句柄但没有成功。我究竟做错了什么?感谢您的任何建议!
<head>
//I'm using joomla and document.js is loading normaly
$document->addScript("path/document.js");
</head>
<body>
<form id='avisar' method='post' name='avisar'><div id='risposta'></div><input type='checkbox' name='invia' id='invia' value='yes' />Check it!</form>
</body>
Document.js:编辑以正确获取变量
$(document).ready(function() {
$('#invia').change(function(){
//variable
var name = $("#att").val();
var user = $("#user_id").val();
var datastr ='name=' + name + '&user=' + user;
var id = parseInt($(this).val(), 10);
if($(this).is(":checked")) {
// checkbox is checked -> do something
$.ajax({
url: "processa_form_avisar.php",
data: datastr,
cache: false,
success: function(html){
alert('Updated successful.');
$('#risposta').fadeIn("slow");
$('#risposta').html(html);
$('#risposta').css("background-color","#e1ffc0");
setTimeout(function() {
$("#risposta").fadeOut("slow");
}, 2000);
}
});
} else {
// checkbox is not checked -> do something different
}
}); });