簡體   English   中英

jQuery單擊事件錯誤:“ TypeError:無法在未定義的情況下調用方法'toString'

[英]jQuery click event error: "TypeError: cannot call method 'toString' on undefined

我的jQuery腳本出現錯誤:

$("input.buttonConfirmOrder").click(function (e) {
    e.preventDefault();
    //do some stuff with the order
    return false;
});

該錯誤將在警報框中彈出:

TypeError:無法在未定義的情況下調用方法“ toString”

我在任何時候都沒有使用toString ,單擊鏈接后即在點擊函數的第一行之前,就會彈出錯誤消息:

e.preventDefault();

調用事件的按鈕如下所示:

<input type="submit" name="ctl00$mainContent$ButtonConfirmOrder" value="Pay" id="ctl00_mainContent_ButtonConfirmOrder" class="buttonConfirmOrder confirmButton">

我已經嘗試了以下所有方法:

  • $("input.buttonConfirmOrder").on("click", function(e) { /...
  • $ = jQuery;
  • jQuery.noConflict();

您的選擇器與您按鈕上的CSS類不匹配。 它應該是:

$("input.buttonConfirmOrderDIBS").click(function (e) {
    e.preventDefault();
    //do some stuff with the order
    return false;
});

我做了一個小提琴來檢查它,它的工作原理是:

http://jsfiddle.net/picklespy/rZ4ZT/2/

的HTML

<input type="submit" name="ctl00$mainContent$ButtonConfirmOrder" value="Pay"
        id="ctl00_mainContent_ButtonConfirmOrder"
         class="buttonConfirmOrder confirmButton">

Javascript:

$("input.buttonConfirmOrder").click(function (e) {
    e.preventDefault();
    alert($(this).val())
    //do some stuff with the order
    return false;
});

如果您唯一的目的是防止默認的提交操作。 然后添加一個屬性onsubmit=return false; 提交按鈕的容器形式。 不需要做e.preventDefault()

暫無
暫無

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

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