jQuery,不要在post后隐藏元素

3hvapo4f  于 2023-05-28  发布在  jQuery
关注(0)|答案(1)|浏览(187)

如何在按下提交后继续显示表单?

<button class="show">Create Team</button>

<form method="post">
<input type="text" name="team_name" />

<input type="submit" value="Submit"></td>
</form>

Jquery:

$(function() {
    $(".show").click(function() {
        $("form").show();
        $(".show").hide();
    });
});
mzillmmw

mzillmmw1#

在这种情况下不需要jQuery。你可以使用简单的JavaScript。
HTML(使用内联CSS):

<button id="hide-show-button" onclick="hideshowform()">Create Team</button>
<form method="post" id="team-form" style="display:none;">
    <input type="text" name="team_name" />
    <input type="submit" value="Submit">
</form>

JavaScript(不是jQuery):

var formstate = "shown";
function hideshowform() {
    if (formstate == "shown") {
        formstate = "hidden";
        document.getElementById("hide-show-button").innerHTML="Create Team"; 
        document.getElementById("team-form").style.display="none"; 
    } else {
        document.getElementById("hide-show-button").innerHTML="Hide Form"; 
        document.getElementById("team-form").style.display="block"; 
        formstate = "shown";
    }
}

或者,如果您不想隐藏窗体,请使用以下代码:
HTML(使用内联CSS):

<button id="show-form-button" onclick="showform()">Create Team</button>
<form style="display:none;" method="post" id="team-form">
    <input type="text" name="team_name" />
    <input type="submit" value="Submit">
</form>

JavaScript(不是JQuery):

function showform() {
    document.getElementById("team-form").style.display="block";
    document.getElementById("show-form-button").style.display="none";
}

相关问题