我使用 jQuery serialize()
创建了一个 URL,它创建了如下内容:
client_number=4&start_date&client_number=5
问题是我想要一个包含这样数组的 url:
client_number[]=4&start_date&client_number[]=5
最佳答案
正在序列化的输入元素的 [name]
必须包含 []
以生成那些 PHP 兼容的查询字符串。
$(function () {
$('form').on('submit', function (e) {
$('pre').text($(this).serialize());
e.preventDefault();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="text" name="example1">
<input type="text" name="example1">
<input type="text" name="example2[]">
<input type="text" name="example2[]">
<input type="submit" value="Serialize">
</form>
<pre></pre>
注意:按键将显示为 %5B%5D
而不是 []
。这是预期的并且可以,因为这是 []
的正确 URL 编码。
关于javascript - 使用数组参数构建 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36480207/