【问题标题】:Dynamic Modal with Form - Undefiend Post Data带表单的动态模态 - Undefiend Post Data
【发布时间】:2014-08-09 18:29:24
【问题描述】:

我一直在研究用户编辑模式,模式显示非常好,但是表单提交无法正常工作。我查看了控制台输出,但找不到我的逻辑失败的地方。我知道我必须创建模态,填充内容,显示模态,然后提交表单。

感谢您的帮助!

代码如下:

var update_user_display = function() {
    $("body").on('click', '.update_user_display', function(evt) {
      evt.preventDefault();

      var user_id = parseInt($(this).attr('data-id'));
      var user_login = $(this).attr('data-user');
      var user_password = $(this).attr('data-password');
      var user_email = $(this).attr('data-email');
      var user_role = $(this).attr('data-role');

      var output = '';
      output += '<table class="user_data table table-striped table-condensed" id="clients">';
      output += '<thead>';
      output += '<tr>';
      output += '<th>Employee Name</th>';
      output += '<th>Password</th>';
      output += '<th>Email Address</th>';
      output += '<th>Role</th>';
      output += '</tr>';
      output += '</thead>';
      output += '<tbody>';
      output += '<form method="post" class="user_edit_form form-horizontal" action="..api/admin/edit_user">';
      output += '<input class="user_id" type="hidden" name="user_id" value="' + user_id + '" />';
      output += '<td><input type="text" id="login" name="login" value="' + user_login + '" /></td>';
      output += '<td><input type="text" id="password" name="password" value="' + user_password + '" /></td>';
      output += '<td><input type="text" id="email" name="email" value="' + user_email + '" /></td>';
      output += '<td><input type="text" id="role" name="role" value="' + user_role + '" /></td>';
      output += '<td><input type="submit" id="submit" name="submit" value="Save" /></td>';
      output += '</form>';
      output += '</tbody>';
      output += '</table>';

      // The BS3 Modal
      var user_modal = $("#modal-user");
      // Populate the Data
      user_modal.find('.modal-title').html('User Editor');
      user_modal.find('.modal-body').html(output);
      // Show the Modal
      user_modal.modal('show');

      // Proccess Data Changes
      $("#submit").on('click', function(evt) {
        evt.preventDefault();

       var form = $(this);
       var url = $(this).attr('action');

       console.log(url);

       var postData = {
           user_id: parseInt($(this).attr('user_id')),
           login: $(this).find('.login').val(),
           password: $(this).find('password').val(),
           email: $(this).find('email').val(),
           role: $(this).find('role').val()
       };

       console.log(postData);

       $.post(url, postData, function(o) {
           if(o.result == 1) {

               Result.success("Successfuly Updated User.");
               // Hide the Modal
              default_modal.modal('hide');
           } else {
               Result.error("No Changes Made.");
          }
       }, 'json');
      });
  });
};

【问题讨论】:

    标签: javascript jquery ajax forms twitter-bootstrap


    【解决方案1】:

    表单提交事件处理程序有一些错误:

    • 每次单击编辑用户时,都会重新绑定提交元素的处理程序 ($('#submit').on('click', function(evt) { });),最终会导致多个处理程序绑定该元素。相反,您应该在 DOM 就绪时使用仅执行一次的事件委托:
      $(document).on('click', '#submit', function(){ })

    • $(this) 对象是被点击的元素 (&lt;input type="submit" id="submit" name="submit" value="Save" /&gt;),而不是表单元素。

    • 为每个表单输入适当的 id。

    • postData.user_id 应该从输入中获取其值,而不是从user_id 属性中获取值,该属性不存在于表单中的任何位置。

    所以正确的代码应该是:

      // Proccess Data Changes
      $(document).on('click', '#submit', function(evt){
        evt.preventDefault();
    
        var $form = $('form.user_edit_form');
        var url = $form.attr('action');
    
        console.log(url);
    
        var postData = {
           user_id: parseInt($('#user_id').val()),
           login: $('#login').val(),
           password: $('#password').val(),
           email: $('#email').val(),
           role: $('#role').val()
        };
    
        console.log(postData);
    
        $.post(url, postData, function(o) {
           if(o.result == 1) {
    
               Result.success("Successfuly Updated User.");
               // Hide the Modal
              default_modal.modal('hide');
           } else {
               Result.error("No Changes Made.");
          }
        }, 'json');
      });
    

    此外,jQuery DOM 树遍历函数对于包裹在 &lt;table&gt; 中的 &lt;form&gt; 元素也无法正常工作:

     <table>
        <form>
           ...
        </form>
     </table> 
    

    你应该使用:

    <form>
         <table>
           ...
         </table>
    </form>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-05
      • 2016-02-23
      • 2017-03-18
      • 1970-01-01
      • 2020-11-16
      • 2016-12-03
      • 2017-11-16
      • 2018-07-23
      相关资源
      最近更新 更多