jquery - 输入点击不起作用

标签 jquery html css

当我单击一个按钮时,我的页面上有一个表单,该表单隐藏了另一个表单。这里我调用了 jQuery 的 show()hide()按钮并更改按钮的 valueid 属性。然后使用按钮新的 id 属性,我在上面使用 click() 函数。但不幸的是,它不起作用。

这是 fiddle .

HTML:

<form id="sign_in">
    <table style="margin: 0 auto;">
        <tr>
            <td align="center"> <span style="font-weight: bold;font-size: 2em;">Sign In</span>

            </td>
            <tr>
                <td>
                    <input class="input" type="email" placeholder="Username" name="username" />
                </td>
            </tr>
            <tr>
                <td>
                    <input class="input" type="password" placeholder="Password" name="password" />
                    <br />
                </td>
            </tr>
            <tr align="right">
                <td>
                    <input class="btn" type="submit" value="Sign In">
                </td>
            </tr>
    </table>
</form>
<form id="sign_up">
    <table style="margin: 0 auto;">
        <tr>
            <td align="center"> <span style="font-weight: bold;font-size: 2em;">Sign Up</span>

            </td>
            <tr>
                <td>
                    <input class="input" type="email" placeholder="Username" name="username" />
                </td>
            </tr>
            <tr>
                <td>
                    <input class="input" type="password" placeholder="Password" name="password" />
                    <br />
                </td>
            </tr>
            <tr align="right">
                <td>
                    <input class="btn" type="submit" value="Sign In">
                </td>
            </tr>
    </table>
</form>
<br/>
<input id="sign_up_btn" class="btn" type="submit" style=" font-weight:bold; height:40px; width: 292px;" value="Create An Account"></input>

JS:

$(document).ready(function () {
    $("#sign_up_btn").click(function () {
        $("#sign_in").hide("slow");
        $("#sign_up").show("slow");
        $(this).attr("value", "Already have an account?");
        $(this).attr("id", "sign_in_btn");
    });
    $("#sign_in_btn").click(function (e) {
        e.stopPropagation();
        $("#sign_up").hide("slow");
        $("#sign_in").show("slow");
        $(this).attr("value", "Create An Account");
        $(this).attr("id", "sign_up_btn");
    });
});

最佳答案

您需要使用事件委托(delegate)因为当您绑定(bind)点击事件时#sign_in_btn不存在

$(document.body).on('click',"#sign_in_btn",function (e) {
        e.stopPropagation();
        $("#sign_up").hide("slow");
        $("#sign_in").show("slow");
        $(this).attr("value", "Create An Account");
        $(this).attr("id", "sign_up_btn");
});

关于jquery - 输入点击不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17384287/

相关文章:

jquery - 禁用初始自动 ajax 调用 - DataTable 服务器端分页

jquery - 如何在折叠/折叠时更改 Bootstrap Accordion 标题

CSS面包屑步骤指示器调整大小问题

javascript - 如何设置选项卡在加载网页时自动显示

javascript - 当元素被 javascript 插入时,css 被忽略

radio 输入组的 jQuery 验证器未在 Bootstrap 中验证

javascript - 根据其他下拉菜单更改下拉菜单

html - 如何在 HTML 中左右对齐文本/段落?

javascript - 使用 show() 显示 div 时显示方法

html - 为什么在 CSS 中使用 ">"?