我有一个 ID 为“addCat”的表单。我正在使用 jquery 将 ID 更改为“editCat”,并使用预先存在的数据填充表单输入,然后提交新表单。
显然新 ID 已更新但表单未提交?
$("#addTags").on("submit",function(){
var dataString = $(this).serialize();;
$.post("./Scripts/adminDoAddCat.php", dataString, function (data) {
if (data == "1") {
$('div.add_result').html("<h3>New Category Added Successfully</h3>").fadeIn(800);
}
if (data == "0") {
alert("Something went wrong");
}
});
return false;
});
$("tbody tr td a.fa-pencil-square").on("click",function(){
$("form#addTags").attr('id','editTags');
alert($('form').attr('id')); //this is the way i make sure ID is updated
});
$("#editTags").on("submit",function(){
var dataString = $(this).serialize();;
alert(dataString); //making form is recognized but it's not working
return false;
});
最佳答案
使用.prop()
而不是.attr()
来更改ID
$("form#addTags").prop('id','editTags');
然后使用 Event Delegation绑定(bind)提交处理程序
$(document).on("submit","#editTags",function(){
var dataString = $(this).serialize();;
alert(dataString);
return false;
});
关于jQuery更改表单ID并提交 "new"表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23805738/