javascript - 使用 JS 单击时不显示表单

标签 javascript jquery html

我已经制作了表单,并使用 CSS 显示属性隐藏它。我有按钮。我想在单击该按钮时显示表单。我已经完成了所有的工作,但仍然没有出现表格。

<input type="button" value="Popup" onclick="showLoginForm();"/>

<form id="loginForm" action="" method="post" style="display:none;">
        <p><strong>ID:</strong> </p>
        <strong>Name: *</strong> <input type="text" id="Name" name="Name" /><br/>
        <strong>Number: *</strong> <input type="text" id="Number" name="Number" /><br/>
        <strong>Email: *</strong> <input type="text" id=""="Email" name="Email" /><br/>
        <input type="submit" id = "submit" name="submit" value="Submit">
        </form>

下面是我的 JS 函数,它没有触发。

    $("button").click(function(e) {
    $("#loginForm").show();

    e.preventDefault();
});

最佳答案

函数showLoginForm()未定义。当您的按钮是输入类型时,您的 jquery 正在监听按钮单击。

$("input[type=button]").click(function(e) {
    $("#loginForm").show();

    e.preventDefault();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<input type="button" value="Popup" />

<form id="loginForm" action="" method="post" style="display:none;">
        <p><strong>ID:</strong> </p>
        <strong>Name: *</strong> <input type="text" id="Name" name="Name" /><br/>
        <strong>Number: *</strong> <input type="text" id="Number" name="Number" /><br/>
        <strong>Email: *</strong> <input type="text" id=""="Email" name="Email" /><br/>
        <input type="submit" id = "submit" name="submit" value="Submit">
</form>

或者使用定义的函数:

function showLoginForm(){ 
    $("#loginForm").show();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<input type="button" value="Popup" onclick="showLoginForm();"/>

<form id="loginForm" action="" method="post" style="display:none;">
        <p><strong>ID:</strong> </p>
        <strong>Name: *</strong> <input type="text" id="Name" name="Name" /><br/>
        <strong>Number: *</strong> <input type="text" id="Number" name="Number" /><br/>
        <strong>Email: *</strong> <input type="text" id=""="Email" name="Email" /><br/>
        <input type="submit" id = "submit" name="submit" value="Submit">
        </form>

关于javascript - 使用 JS 单击时不显示表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31171158/

相关文章:

javascript - 如何创建自定义 <select>?

javascript - 几秒钟后显示/隐藏 HTML 移动按钮

Javascript 计算未返回正确的金额?

angularjs - 使用自签名证书对服务器进行 ajax 调用。

jquery - 悬停在 child 身上时如何禁用 parent 悬停

javascript - notepad++ 正则表达式 将 "00"替换为 "01"、 "02"、 "03"等

javascript - 使用 JavaScript 显示一个 div

javascript - Ajax 成功功能无法正常工作

html - 为什么我的 HTML 没有居中对齐?

javascript - window.onresize 只调用一次