由jQuery serialize()/serializerary()发布,不包括文本区域



通过jQuery serialize方法发布时,除文本区域外,所有数组元素都将被捕获。我从var_dump和Firebug中验证了相同的结果。

$(function() {
$("#btnJQ").on("click", function() { //100
var jqxhr = 'a=crea_inv&' + $('#frmData').find('input[name^="empl"]').serialize();
console.log(jqxhr)
$.post('jq.php', {
'empl': jqxhr
}, function(response) {
$(".result").html(response);
}); //.100
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="frmData" action="" method="post">
<table>
<tr>
<td>ID</td>
<td>First Name</td>
<td>Last Name</td>
</tr>
<tr>
<td><input type="text" name="empl[0][id]" value="2135" /></td>
<td><input type="text" name="empl[0][first_name]" value="John" /></td>
<td><input type="text" name="empl[0][last_name]" value="Doe" /></td>
</tr>
</table>
<textarea id="lnotes" name="empl[0][lnotes]" rows="4" cols="50">
At .com you will learn how to make a website. We offer free tutorials in all web development technologies.
</textarea>
<br>
<button id="btnJQ" type="button">JQ</button>
<input type="submit" name="Submit" />
</form>

然而,使用标准提交时,显示textarea值。

<?php
//parse array to readable str
$post_data = $_POST['empl'];
parse_str($_POST['empl'],$new_data);
//Now We have the new array saved in a variable called $new_data
print_r($new_data);
?>

var_dump和firebug的结果。

Array
(
[a] => crea_inv
[empl] => Array
(
[0] => Array
(
[id] => 2135
[first_name] => John
[last_name] => Doe
)
)
)

按标准表格提交的结果

Array ( [0] => Array ( [id] => 2135 [first_name] => John [last_name] => Doe [lnotes] => At .com you will learn how to make a website. We offer free tutorials in all web development technologies. ) ) At .com you will learn how to make a website. We offer free tutorials in all web development technologies.

textarea不是input

您可以使用$(":input[name^=empl]")或仅使用$("[name^=empl]")

$(function() {
$("#btnJQ").on("click", function() { //100
$('#frmData').find('[name^="empl"]').each(function() { console.log(this.name)} )
var jqxhr = 'a=crea_inv&' + $('#frmData').find('[name^="empl"]').serialize();
console.log(jqxhr)
$.post('jq.php', {
'empl': jqxhr
}, function(response) {
$(".result").html(response);
}); //.100
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="frmData" action="" method="post">
<table>
<tr>
<td>ID</td>
<td>First Name</td>
<td>Last Name</td>
</tr>
<tr>
<td><input type="text" name="empl[0][id]" value="2135" /></td>
<td><input type="text" name="empl[0][first_name]" value="John" /></td>
<td><input type="text" name="empl[0][last_name]" value="Doe" /></td>
</tr>
</table>
<textarea id="lnotes" name="empl[0][lnotes]" rows="4" cols="50">
At .com you will learn how to make a website. We offer free tutorials in all web development technologies.
</textarea>
<br>
<button id="btnJQ" type="button">JQ</button>
<input type="submit" name="Submit" />
</form>

最新更新