我在这里找到了类似的问题,但对我没有任何帮助。
输入如下:
<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]"]')
Answers:
使用选择器开头
$('input[name^="pages_title"]').each(function() {
alert($(this).val());
});
注意:与@epascarello一致,更好的解决方案是将一个类添加到元素中并引用该类。
这通常适用于“复选框”和“单选”按钮...但是,每个名称应相同。
<input type="text" value="2" name="pages_title[]">
<input type="text" value="1" name="pages_title[]">
var x = $('input[name="pages_title[]"]').val();
“假设”是为了让您使用逗号分隔值的所有字段。从来没有尝试过使用文本框,所以不能保证。
正如@John所说:遍历它们。
$('input[name="pages_title[]"]').each(function() { var aValue = $(this).val(); });
编辑:并非总是您只需要回答OP问题,有时您需要教给他们更好的方法。(抱歉,这听起来很嚣张)
评论:定义输入为
<input type="text" value="1" name="pages_title[1]">
<input type="text" value="2" name="pages_title[2]">
正在破坏数组,每个输入都有一个唯一的名称,因此,它不再是数组。
$('input[name="pages_title[]"]').each(function() { var aValue = $(this).val(); });
[1]然后表示另一个数组[2]
name="pages_titles"呢?另外,为什么要使用一个类,当其清除时name应该相同?有趣的额外标记?
^=比类选择器慢很多。它必须解析字符串并检查是否匹配。我不知道您对name="pages_titles"
pages_titles(而不是pages_title)。我同意他应该将他的选择范围缩小到类似,#divId[name='pages_title']但是为什么要添加一个类呢?差异将很小,不会被注意到。
add class解决方案,他将不会在服务器端获得任何结果。
您可以给输入文本框类名称,如下所示:
<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]。
您可以使用包含选择器:
[name * =” value”]:选择具有指定属性的元素,该元素的值包含给定的子字符串。
$( document ).on( "keyup", "input[name*='pages_title']", function() {
alert($(this).val());
});
为了通过具有特定特征的属性选择元素,您可以使用正则表达式模式创建一个新的选择器,如以下代码片段所示。regex的使用旨在使新选择器尽可能灵活:
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[]">