簡體   English   中英

避免在用戶單擊html按鈕時回發

[英]Avoid posting back when user clicks html button

我想避免在用戶單擊html按鈕時回發。 因為我想使用javascript顯示一個彈出窗口。 單擊按鈕時,我不希望回發。

function ShowEditChoicesPopup() {
    // I want to show some pop up here.
    $("popupdiv").show();
    return false;
}

標記正在使用的是:

<button onclick="javascript:ShowEditChoicesPopup();"> Edit Choices </button>

請提出一些想法。

套用event.preventDefault();

 function ShowEditChoicesPopup(event) {
   event.preventDefault();

    $("popupdiv").show();

 }

嘗試:

function ShowEditChoicesPopup() {
     // I want to show some pop up here
     $("popupdiv").show();
        return false;
     }
    return false;
}

您需要為ShowEditChoicesPopup()再次return false ,這應該取消表單提交。 假設代碼在<form>塊中。

請嘗試使用以下代碼段。

<button onclick="javascript:return ShowEditChoicesPopup();">

要么

<button onclick="javascript:return ShowEditChoicesPopup(); return false;">

要么

<button onclick="javascript:ShowEditChoicesPopup(); return false;">

使用event.preventDefault(); 這將防止元素的默認行為並執行指定的功能

根據例子:

http://jsfiddle.net/guinatal/ct8uS/1/

HTML

<form>
    <button onclick="return ShowEditChoicesPopup();"> Edit Choices </button>
    <div id="popupdiv" style="display:none">popupdiv</div>
</form>

JS

function ShowEditChoicesPopup() {
     // I want to show some pop up here
     $("#popupdiv").show();

    return false;
}

暫無
暫無

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

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