簡體   English   中英

將jQuery click事件綁定到常規javascript按鈕

[英]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選擇器的完整列表,請使用以下命令:

http://www.w3schools.com/jquery/jquery_ref_selectors.asp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM