簡體   English   中英

如何不在jQuery中提交表單

[英]How to not to submit form in jQuery

我有一個關於我在我的網站上嘗試的代碼的問題。 我想最小化寫作的大小,所以我試圖通過簡單的點擊隱藏對象。 但這確實提交了我的表格。

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#hide").click(function(){
        $("table").hide();
    });
    $("#show").click(function(){
        $("table").show();
    });
});
</script>
</head>
<body>


<table style="border:solid #FFFFFF 2px;
background-color:#0000ff;height:1px;width:300px;
text-align:left;" "empty-cells:hide;" border="1" cellpadding="1" cellspacing="1">
    <tr>
        <th>Test</th>
        <th>Extras</th>
        <th>Extras</th>
    </tr>
</table>
<br>

<button id="hide">Hide</button>
<button id="show">Show</button>

</body>
</html>

[submit class:button id:form-submit "Send"]

您需要為事件使用.preventDefault()函數。 單擊按鈕時會阻止發送。 請注意,您需要在參數(e)中發送事件。

一個例子:

$("#hide").click(function(e){
    e.preventDefault();
    $("table").hide();
});

這是一個縮短腳本的解決方案

 $(document).ready(function(){ $("#switch").click(function(){ $("table").toggle(); $("#switch").text(($("#switch").text() == "Hide") ? "Show" : "Hide"); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table style="border:solid #FFFFFF 2px; background-color:#0000ff;height:1px;width:300px; text-align:left;" "empty-cells:hide;" border="1" cellpadding="1" cellspacing="1"> <tr> <th>Test</th> <th>Extras</th> <th>Extras</th> </tr> </table> <br> <button id="switch">Hide</button> 

在按鈕中添加type="button"

<button type="button" id="hide">Hide</button>
<button type="button" id="show">Show</button>

嗯,這里沒有元素,現在顯示和隱藏工作正常。 但也許當你以某種形式使用它時,問題就會發生。 要解決該問題,請使用以下內容:

$(document).ready(function(event){
    $("#hide").click(function(){
        $("table").hide();
        return false;
    });
    $("#show").click(function(){
        $("table").show();
        return false;
    });
});

- 要么 -

$(document).ready(function(event){
    event.preventDefault();
    $("#hide").click(function(){
        $("table").hide();
    });
    $("#show").click(function(){
        event.preventDefault();
        $("table").show();
    });
});

沒有任何進一步的問題,這更容易

 > <button title="Click to show/hide content" type="button" > onclick="if(document.getElementById('Funktionen') > .style.display=='none') {document.getElementById('Funktionen') > .style.display=''}else{document.getElementById('Funktionen') > .style.display='none'}">+</button> <div id="Funktionen" > style="display:none;"> HERE COMES IN WHATEVER YOU WANT TO HIDE WITH > the + Button </div> 

暫無
暫無

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

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