简体   繁体   English

用jquery附加一个值到表单并为AJAX Post序列化表单

[英]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. 我有一个表单,该表单的输入由用户填充,并作为带有AJAX Post请求的序列化表单发送。 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). 我想在发送值之前将值追加到表单中,但似乎不起作用(我使用了Chrome的调试器,可以看到唯一不起作用的部分是追加部分)。 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. console.log()部分中,它仅显示用户输入的输入。

The problem is that you're appending the value of your EmailOrPhoneNumber input to the form, instead of the input element itself. 问题是您要将EmailOrPhoneNumber输入的值附加到表单,而不是输入元素本身。

The .serialize() function can only serialize the values of form elements. .serialize()函数只能序列化表单元素的值。

Try removing .val(); 尝试删除.val(); from your phoneNumber declaration. 从您的phoneNumber声明中。

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

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