jquery - 从 ajax 加载的表单提交

标签 jquery html

你好

有任何关于提交 ajax 加载表单的想法: www.example.com/form.php 是

<form>
    <fieldset>
        <legend>Your name?</legend>
        <input type="name" id="name" />
        <button type="submit">OK!</button>
    </fieldset>
</form>

www.example.com/index.php 是

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Myform</title>
        <script type="text/javascript" src="jquery.js"></script>
    </head>
    <body>
        <div id="formDiv"></div>
        <button id="open">Open</button>
        <button id="close">Close</button>
        <script type="text/javascript">
            (function($) {
                $(document).ready(function() {
                    $('#open').click(function() {
                        $('#formDiv').load('www.example.com/form.php');
                    });
                    $('#close').click(function() {
                        $('#formDiv').empty();
                    });
                    $('#formDiv form').submit(function() {
                        alert("Hello, "+($('#formDiv form #name').val())+"!");
                        return false;
                    });
                });
            })(jQuery);
        </script>
    </body>
</html>

我希望它提醒“你好,我的名字”而不是提交。

谢谢

最佳答案

使用event delegation为动态加载的表单设置事件处理程序:

$('#formDiv').on('submit', 'form', function(event) {
    alert("Hello, "+($('#name').val())+"!");
    return false;
});

关于jquery - 从 ajax 加载的表单提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15357787/

相关文章:

javascript - jQuery - 当输入不是来自自动完成给出的列表时禁用回车键

带有 css float 两列布局的 JQuery UI 选项卡标题太大

java - 关于选择选择框

java - Jsoup select方法返回null,当标签存在于网页中

javascript - 防止已经改变颜色的单元格再次改变颜色

javascript - 将 CSS 应用于 printWindow.print();在 JavaScript 中

javascript - 向表中添加一行会导致 header 中的元素移动到 tbody 中

javascript - jQuery : How can showint text and hide then showing another?

javascript - $(window).scroll 的更快替代品?

javascript - Yajra 员工 :140 Uncaught TypeError: $(. ..).DataTable 不是函数