简体   繁体   中英

Dynamic Hidden Form Field Data Absent From POST

I have a form that, when submitted, does not post the dynamically added hidden field.

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:

$('#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:

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 . For whatever reason, it isn't POSTing.

EDIT: As @JayBlanchard pointed out below, I am adding to the form after the POST has been written.

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.

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;
        });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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