[英]Bind jQuery click event to regular javascript button
我正在尝试在jquery中创建一个简单的函数,如果单击“取消”按钮,则将用户的浏览器定向到上一页,但由于我对jquery的经验很少,我对如何操作有点了解。
<input type="button" id="cancelBc2" name="cancelForm" value="Cancel">
$(document.ready(function() {
$('cancelBc2').click(function() {
var cancel = confirm("Are you sure you want to leave the page?");
if (cancel == true) {
window.location = "chooseConfig.php";
}
});
});
我错过了什么或者页面完全忽略了
$(document.ready(function()?
你错过了选择器上的#
$('cancelBc2') //looking for an element <cancelBc2 />
$('#cancelBc2') //looking for an element with id="cancelBc2"
你有一个错字
$(document.ready
应该
$(document).ready(function()
甚至更好
$(function(){});
应该添加前缀#以在jQuery中选择Id
$('#cancelBc2').click(function() {
var cancel = confirm("Are you sure you want to leave the page?");
if (cancel == true) {
window.location = "chooseConfig.php";
}
});
有关信息: http : //api.jquery.com/id-selector/
你的代码很好,但你错过了正确的选择器。 它应该是$('#cancelBc2')
这将选择DOM中具有id cancelBc2
有关jQuery选择器的完整列表,请使用以下命令:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.