我在这里找到了类似的问题,但对我没有任何帮助。
输入如下:
<input type="text" value="2" name="pages_title[1]">
<input type="text" value="1" name="pages_title[2]">
尝试获取这些字段,例如:
$('input[name="pages_title[]"]')
但结果为空:(
如何获得所有领域?我只能得到它喜欢$('input[name="pages_title[1]"]')
我在这里找到了类似的问题,但对我没有任何帮助。
输入如下:
<input type="text" value="2" name="pages_title[1]">
<input type="text" value="1" name="pages_title[2]">
尝试获取这些字段,例如:
$('input[name="pages_title[]"]')
但结果为空:(
如何获得所有领域?我只能得到它喜欢$('input[name="pages_title[1]"]')
使用带选择器的开头
$('input[name^="pages_title"]').each(function() {
alert($(this).val());
});
注意:与@epascarello 一致,更好的解决方案是向元素添加一个类并引用该类。
const textValues = $.map($('input[type=text][name="pages_title[]"]'), function(el) { return el.value; });
console.log(textValues);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" value="Hello" name="pages_title[]">
<input type="text" value="World" name="pages_title[]">
使用 map 方法,我们可以获得存储在数组中的输入值。
var values = $("input[name='pages_title[]']").map(function(){return $(this).val();}).get();
将输入名称放在单引号之间,以便将括号[]视为字符串
var multi_members="";
$("input[name='bayi[]']:checked:enabled").each(function() {
multi_members=$(this).val()+","+multi_members;
});
您可以给输入文本框类名称,如下所示:
<input type="text" value="2" name="pages_title[1]" class="pages_title">
<input type="text" value="1" name="pages_title[2]" class="pages_title">
并像这样迭代:
$('input.pages_title').each(function() {
alert($(this).val());
});
我认为最好的方法是使用 Propper Form 并使用jQuery.serializeArray。
<!-- a form with any type of input -->
<form class="a-form">
<select name="field[something]">...</select>
<input type="checkbox" name="field[somethingelse]" ... />
<input type="radio" name="field[somethingelse2]" ... />
<input type="text" name="field[somethingelse3]" ... />
</form>
<!-- sample ajax call -->
<script>
$(document).ready(function(){
$.ajax({
url: 'submit.php',
type: 'post',
data: $('form.a-form').serializeArray(),
success: function(response){
...
}
});
});
</script>
这些值将在 PHP 中以$_POST['field'][INDEX].
最常用的是这个:
$("input[name='varname[]']").map( function(key){
console.log(key+':'+$(this).val());
})
你得到了数组位置的键和值。
您可以使用包含选择器:
[name*=”value”]:选择具有指定属性且值包含给定子字符串的元素。
$( document ).on( "keyup", "input[name*='pages_title']", function() {
alert($(this).val());
});
您可以使用双反斜杠转义方括号,如下所示:
$('input[name="pages_title\\[\\]"]')
为了通过具有特定特征的属性选择元素,您可以使用正则表达式模式创建一个新的选择器,如下面的片段所示。正则表达式的使用旨在使新选择器尽可能灵活:
jQuery.extend(jQuery.expr[':'], {
nameMatch: function (ele, idx, selector) {
var rpStr = (selector[3] || '').replace(/^\/(.*)\/$/, '$1');
return (new RegExp(rpStr)).test(ele.name);
}
});
//
// use of selector
//
$('input:nameMatch(/^pages_title\\[\\d\\]$/)').each(function(idx, ele) {
console.log(ele.outerHTML);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="2" name="pages_title[1]">
<input type="text" value="1" name="pages_title[2]">
<input type="text" value="1" name="pages_title[]">
另一种解决方案可以基于:
[name^=”value”]:选择具有指定属性的元素,其值恰好以给定字符串开头。
.filter():将匹配元素集减少为匹配选择器或通过函数测试的元素。
var selectedEle = $(':input[name^="pages_title"]').filter(function(idx, ele) {
//
// test if the name attribute matches the pattern.....
//
return /^pages_title\[\d\]$/.test(ele.name);
});
selectedEle.each(function(idx, ele) {
console.log(ele.outerHTML);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="2" name="pages_title[1]">
<input type="text" value="1" name="pages_title[2]">
<input type="text" value="1" name="pages_title[]">
var data = $("input[name='page_title[]']")
.map(function () {
return $(this).val();
})
.get();