[英]Post serialized form data and extra variables using JQuery
我試圖使用這段代碼序列化一個表單並同時發送一個在表單中找不到的額外變量。 以下代碼行是我所期望的,但遺憾的是不起作用。
var thePage = theFilename();
$.post("pagedetail.php", { $("#PageDetailForm").serialize(), thePage: thePage },
function(data) {
alert(data);
});
有任何想法嗎?
var serialized = $('#PageDetailForm').serialize();
serialized.thePage = thePage;
$.post("pagedetail.php", serialized,
function(data) {
alert(data);
});
你可以做的是將額外的數據添加到隱藏的輸入並在pagedetail.php
頁面中捕獲它。
例如拉特說你的表格
<form id='PageDetailForm'>
<input type="hidden" name="value" id="value" value="the value u wamnt to add goes here" />
....other inputs
</form>
在此之后,只需執行正常的$.post
$.post("#pagedetail.php",$("#PageDetailForm").serialize(),function(data){
$("#ans").html(data);
// in the pagedetail.php
$echo $_POST['value'];
如果你仍然困惑我@dplumptre
希望不幫助
嘗試使用$.post
的第二個參數:
{ form: $("#PageDetailForm").serialize(), thePage: thePage }
希望你還需要這個:)。 嘗試使用serializeArray()方法,然后在結果數組中推送一些額外的數據,這樣就不會有分割數組等了:
var postData = $('#form-id').serializeArray();
var additionalData = $('#additionalDataID').val();
postData.push({name: 'additionalName', value: additionalData});
最后:
$.post(URL, postData);
嘗試sortable('toArray')
:
var thePage = theFilename();
$.post("pagedetail.php", { pageDetailForm: $("#PageDetailForm").sortable('toArray'), thePage: thePage },
function(data) {
alert(data);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.