我正在调用一个 jQuery 函数,但警报函数显示 [object Object]。检查值有效,但传递的 id 值无效。
下面是对从 html 输出中获取的函数的调用:
<td class="myGroups">
<input class="chkSubscribed" type="checkbox" onclick="UpdateSubscription(2)" name="subscribed">
</td>
这是 javascsript 函数:
$(".chkSubscribed").change(function(id) {
alert(id);
var chkd=0;
var groupid=id;
if (this.checked) { chkd=1; }
$.ajax({
type:"POST",
url:"resources/updateSubscribe.php",
data: { group_id: groupid, checkval: chkd },
success: function(){
alert("Account updated!" + "chkval=" + chkd + " id=" + groupid); }
});
});
感谢您的帮助!
最佳答案
你做错了。
要么你想调用一个函数,要么你想使用 change 事件。
如果你想调用一个函数创建一个函数如下:
function moo(id) {
//code here
}
如果你想使用 change 事件来做到这一点,你可以给你的输入一个你想要的数字的 id,如下所示:
<input id="2" class="chkSubscribed" type="checkbox" name="subscribed">
然后您的 javascript 应该如下所示:
$(".chkSubscribed").change(function() {
id=this.id;
//and the rest of the code as you wrote
});
关于javascript - 将值传递给 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7139919/