简体   繁体   中英

append a value to form with jquery and serialize the form for AJAX Post

I have a form that has an input that gets filled by the user and is sent as a serialized form with an AJAX Post request. I want to append a value to my form before sending it but it seems like it is not working (I have used Chrome's debugger and I can see that the only part that is not working is the append part). Here is my code:

 var $form = $("#confirmPhoneNumberForm");
    var phoneNumber = $("input[name='EmailOrPhoneNumber']").val();
    $form.append("PhoneNumber", phoneNumber);
    var formlog = $form.serialize();
    console.log(formlog);

In the console.log() part it just shows the input that the user enters.

The problem is that you're appending the value of your EmailOrPhoneNumber input to the form, instead of the input element itself.

The .serialize() function can only serialize the values of form elements.

Try removing .val(); from your phoneNumber declaration.

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