gpt4 book ai didi

javascript - 从 onclick 事件调用 jQuery 函数

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:35:46 24 4
gpt4 key购买 nike

由于一些奇怪的要求和 jQuery 在我们的应用程序中的实现方式,我必须通过复选框 onclick 事件调用 jQuery 函数。

下面是通过 div ID 触发的函数的完美实现。但是,同样的代码在我的应用程序中不起作用。

在我的应用程序中,我使用的是 jQuery 1.7.1 版。我没有收到任何错误,该功能根本不会触发。我正在使用 Chrome 进行调试。当我尝试在 onclick 中调用它时,它会响应,但会返回 undefined

HTML

<div id="dialog-confirm" title="Select Options">
<!--I need to call function in onclick event for checkbox below-->
<input type="checkbox" id="chkall" /> Check/Uncheck
<br /><br />

<input type="checkbox" />Option 1<br />
<input type="checkbox" />Option 2<br />
<input type="checkbox" />Option 3<br />
<input type="checkbox" />Option 4<br />
<input type="checkbox" />Option 5<br />
<input type="checkbox" />Option 6<br />
<input type="checkbox" />Option 7
</div>

JS

$(function() {
$( "#dialog-confirm" ).dialog({
resizable: false,
height:350,
modal: true,
buttons: {
"Go": function() {
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});

$(document).ready(function(){
$('#chkall').click(function() {
// this is the function I need to call
var opt = $(this).parent().find('input[type=checkbox]');
opt.prop('checked', $(this).is(':checked') ? true : false);
});
});

最后, fiddle 链接

http://jsfiddle.net/uxRGB/1/

最佳答案

使用change事件代替click

$('#chkall').change(function() {

如果还是不行,你可以用这个:

<input type="checkbox" id="chkall" onclick="myfunc()" />

并且:

function myfunc () {
// this is the function I need to call
var opt = $("#chkall").parent().find('input[type=checkbox]');
opt.prop('checked', $("#chkall").is(':checked') ? true : false);
}

关于javascript - 从 onclick 事件调用 jQuery 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18043444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com