简体   繁体   English

POST缺少动态隐藏表单字段数据

[英]Dynamic Hidden Form Field Data Absent From POST

I have a form that, when submitted, does not post the dynamically added hidden field. 我有一个表单,提交时不会发布动态添加的隐藏字段。

html: 的HTML:

<div>
<form action="thankyou.php" onsubmit="return validate()" id="orderform" method="post">
<input type="text" name="name" /><br>
<input type="text" name="email" required /><br>
<input type="text" name="charterco" required /><br>
<input type="text" name="bname" /><br>
<input type="text" name="dtime" required /><br>
<input type="submit" />
</form>
</div>

jquery: jQuery的:

$('#orderform').submit(function(eventObj){
$('<input />').attr('type','hidden')
    .attr('id','list')
    .attr('name','shopList')
    .attr('value',sliststr>)
    .appendTo('#orderform');
return true;
});

POST data from Chrome DevTools: 来自Chrome DevTools的POST数据:

name:b
email:b@b.com
charterco:b
bname:b
dtime:12:00
message:Comment

I can't work out what's gone wrong. 我无法解决出了什么问题。 My sliststr variable turns up filled and correct in my little debugging test on jsfiddle here . 我sliststr变量变成了填充和正确的对的jsfiddle我的小调试测试这里 For whatever reason, it isn't POSTing. 出于某种原因,它不是POST。

EDIT: As @JayBlanchard pointed out below, I am adding to the form after the POST has been written. 编辑:正如@JayBlanchard在下面指出的那样,我已在编写POST后添加到表单。

Try to append the dynamic element, set the value of it and then submit the form. 尝试附加动态元素,设置其值,然后提交表单。 Otherwise it's submitting the form and then appending the html in callback. 否则,它将提交表单,然后在回调中附加html。

Try the following. 尝试以下方法。

function validate(){
        var shoplist = [1,2,3];
        $('#orderform').append("<input type='text' name='shop' id='list'>")
        $('[name="shop"]').val(shoplist)
        $('#orderform').submit(function(eventObj){

        return true;
        });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM