如何向使用jQuery ajax的服務器發佈表單數據以及其他一些值(即{a:'A',b:'B',c:'C'}
)?向服務器發佈帶有額外值的表單數據
HTML代碼:
<form id="myForm">
<p>firstname: <input name="firstname" type="text" value="John" /></p>
<p>lastname: <input name="lastname" type="text" value="Doe" /></p>
</form>
Javascript代碼:
var data={a:'A',b:'B',c:'C'};
$.post('index.php',data); //This seems to work
var formData=$('#myForm').serializeArray();
$.post('index.php',formData); //This seems to work
//Wish to post {a:'A',b:'B',c:'C',firstname:'John',lastname:'Doe'} to the server
//Doesn't work
$.extend(data, formData);
console.log(data);
$.post('index.php',data);
想我可以單獨推'{A: 'A',B: 'B',C: 'C'}',但似乎應該有一個更好的辦法。有沒有替代'serializeArray()'不返回數組名/值? – user1032531