需要更新表单数据并在同一功能中 - 如果数据正确则提交。 UPD:一切正常,修正了拼写错误
onClick函数中的流程:
$.each( result.post_vals, function( entry, new_val ) {$('#' +entry).val(new_val);})
)
$('#form').attr('action', response.result.new_form_action_url)
)
$('#' +entry).val(new_val)
调用更新的值在表单 URL 更新时没有提交。我尝试了多种方法玩了3天,但都失败了。
有什么想法吗?
代码(稍微简化)如下
// HTML
<form name="cart_form" id="cart_form" class="form-inline" method="post">
<input type="hidden" name="pp_biz_id" value="">
<input type="hidden" name="pp_user_guid" value="">
<input type="hidden" name="pp_parm1" value="">
<input type="hidden" name="orderid" value="">
.....
<input type="hidden" name="pp_doc_name" value="">
<input type="button" name="checkout" value="DO IT">
</form>
//JS
$(document).on('click','[name="checkout"]',function() {
//e.preventDefault();
console.log('shopping_cart_save_addr')
var form = $('#cart_form');
var pageData = form.serialize()
console.log(pageData);
$.ajax({
type: "POST",
url: "get_data.php",
data: pageData,
dataType: 'json',
success: function (response) {
console.log(response);
if (response.success) {
if (response.result) {
if (response.result.form_url) {
$('#cart_form').attr('action', response.result.form_url);
}
if (response.result.html) {
$('#result').html = response.result.html;
}
if (response.result.post_vals) {
console.log(response.result.post_vals)
$.each(response.result.post_vals, function (entry, new_val) {
$('#' + entry).val(new_val);
console.log(entry + ': ' + new_val);
if (entry && new_val) {
var el = document.getElementsByName(entry);
if (el && new_val) {
if (typeof new_val === 'string') {
new_val = new_val.trim()
}
el[0].setAttribute('value', new_val);
}
}
});
}
}
console.log($('#cart_form').serialize())
$('#cart_form').submit()
} else {
console.log(response);
}
if (response.result.html) {
$('#result').html = response.result.html;
}
}
);});});
问题:
success: function(response) {
console.log(response);
if(response.success){
if(response.result){
if (response.result.form_url) {
$('#cart_form').attr('action', response.result.form_url);
}
if (response.result.post_vals) {
console.log(response.result.post_vals)
$.each( response.result.post_vals, function( entry, new_val ) {
$('#' +entry).val(new_val);
console.log(entry + ': ' + new_val);
if(entry && new_val) {
var el = document.getElementsByName(entry);
if(el && new_val) {
if (typeof new_val === 'string') {
new_val = new_val.trim()
}
el[0].setAttribute('value', new_val);
}
}
});
}
}
console.log($('#cart_form').serialize())
$('#cart_form').submit()
}
if (response.result.html) {
$('#result').html = response.result.html;
}
}