[英]Dynamic Modal with Form - Undefiend Post Data
I've been working on a user edit modal, the modal display's perfectly fine however the form submission is not working properly. 我一直在研究用户编辑模式,模式显示非常好,但是表单提交不能正常工作。 I've looked at console output and I can't spot where my logic is failing me. 我已经看过控制台的输出,但是我找不到逻辑失败的地方。 I know I have to create the modal, fill the content, display the modal and then submit the form. 我知道我必须创建模式,填充内容,显示模式,然后提交表单。
Thanks for the help! 谢谢您的帮助!
Here is the code: 这是代码:
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');
});
});
};
There are some errors in the form submit event handler: 表单提交事件处理程序中存在一些错误:
every time you click to edit a user, you rebind the handler of the submit element( $('#submit').on('click', function(evt) { });
), ending up with multiple handlers bound that element. 每次单击以编辑用户时,都将重新绑定Submit元素的处理程序( $('#submit').on('click', function(evt) { });
),最后将多个处理程序绑定到该元素。 Instead, you should use event delegation, which is only executed once, on DOM ready: 相反,您应该在DOM就绪状态下使用仅执行一次的事件委托:
$(document).on('click', '#submit', function(){ })
the $(this)
object is the element being clicked ( <input type="submit" id="submit" name="submit" value="Save" />
), and not the form element. $(this)
对象是要单击的元素( <input type="submit" id="submit" name="submit" value="Save" />
),而不是form元素。
give each form input appropiate ids. 给每个表单输入适当的ID。
postData.user_id
should get its value from an input, not from user_id
attribute, which is not present anywhere in form. postData.user_id
应该从输入中获取其值,而不是从user_id
属性中获取,后者在表单中的任何地方都不存在。
So the correct code should be: 因此正确的代码应为:
// 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');
});
Also jQuery DOM tree traversal functions will not work properly for a <form>
element wrapped in a <table>
: 此外,对于包装在<table>
的<form>
元素,jQuery DOM树遍历功能将无法正常工作:
<table>
<form>
...
</form>
</table>
Instead you should use: 相反,您应该使用:
<form>
<table>
...
</table>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.