jquery 从对话框发布表单

标签 jquery jquery-ui

我有一个js函数,它获取html以从 Controller 创建新员工,将其插入表单标签内(用于稍后的.serialize()),然后将此html插入到createDialog div并将此div显示为对话框。

<div id="createDialog" style="display:none;">
</div>

$.get('/Employee/Create',
     function (html) {
        html = "<form id='createEmp'>" + html;
        html = html + "</form>";
        $("#createDialog").html(html);
        $("#createDialog").dialog({
            modal: true,
            width: 500,
            buttons: { "Save": function () { postEmployee(); } },
            close: function (ev, ui) { $(this).dialog('destroy'); }
        });
     });


    function postEmployee() {

        $.post('/Employee/Create',
        $("#createEmp").serialize(),
        function (html) {
            $('#reply').html(html);
        });
    }

这有效,但只能一次。对于下一篇文章,之前文章中的所有表单字段也会添加到当前文章中。 这个问题可以解决吗?

谢谢你!

最佳答案

您还需要删除 <form>您创建的元素,如下所示:

close: function (ev, ui) { $(this).dialog('destroy').empty(); }

您还可以使用 .wrapInner() 使整个函数更加简洁。像这样:

$.get('/Employee/Create', function (html) {
  $("#createDialog").html(html).wrapInner("<form id='createEmp'> />");
  $("#createDialog").dialog({
      modal: true,
      width: 500,
      buttons: { "Save": postEmployee },
      close: function (ev, ui) { $(this).dialog('destroy').empty(); }
  });
});

关于jquery 从对话框发布表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3914953/

相关文章:

javascript - 使用数据属性的 jQuery 搜索

jquery - 防止 jQuery datepicker 自动设置日期

jquery - 如何通过ajax将项目ID发送到服务器: jquery sortable

javascript - 表单未通过 jQuery 提交

javascript - JSON 在 FireFox 和 Safari (Windows Vista) 上返回空响应

JavaScript 函数传递?

jquery-ui - Jquerymobile ajax 响应元素不接受 css

javascript - Jquery JSON解码多个数组

javascript - 使用ajax时,jquery on click事件不起作用

javascript - JQuery ajax stoppen in $.when 服务器返回 500 错误