用jquery获取数组的值 [英] Obtain values of array with jquery
问题描述
< form method =postname =b>
< table>
< tr>
< td>字段A< / td>
< td>< input type ='text'name ='field [0] [a]'id ='field [0] [a]'>< / td>
< td>字段B< / td>
< td>< textarea name ='field [0] [b]'id ='field [0] [b]'>< / textarea>< / td>
< td><按钮>移除< /按钮>< / td>
< / tr>
< / table>
< / div>
< div id =container>< / div>
< button id =mybutton>新增表格< /按钮>
< div align =center>
< p>< button onClick =dadosFormulario()value =Registarname =registar> Registo< / button>< / p>
< / div>
< / form>
我有一个jquery函数,用于复制我点击添加表单按钮时的输入。所以,关于这一点,我试图用jquery的.val()方法保存输入的值。
因此,如果我有像这样的输入one:
< input type ='text'name ='teste'id ='teste'>
我知道这样做:
var v = $('#teste')。val();
我可以收到她的价值。然而,如果我有一个输入数组,并且这是我的情况,因为我可以复制表单的字段,我如何从以下输入中获取多个值?
< input type ='text'name ='field [0] [a]'id ='field [0] [a]'>
< input type ='text'name ='field [0] [b]'id ='field [0] [b]'>
var v = $('#field [iterator] [a]')。val();不会工作,它会导致'未定义'。
jquery函数,其中我无法从val();给出undefined:
$ countForms = 1;
(function($){
$ .fn.addForms = function(idform){
var myform =< table> +
< tr>+
< td>字段A(+ $ countForms +):< / td>+
< td><输入type ='text'name ='field\\ [+ $ countForms +\\''>< / td>+
< td>字段B(+ $ countForms +):< / td>+
< td><textarea name ='field [+ $ countForms +] ['b']' >< / textarea>< / td>+
< td><按钮>删除< /按钮>< / td>+
< / tr> +
< / table>;
if(idform =='mybutton'){
myform = $(myform);
$(button,$(myform))。click(function(){$(this).parent()。parent()。remove(); });
$(this).append(myform);
$ countForms ++;
}
};
})(jQuery);
$ b $(函数(){
$(#mybutton)。bind(click,function(e){
e.preventDefault(); $ b $如果($ countForms< 3){
$(#container).addForms(idform);
}
(b变量idform = this.id;
});
});
方括号用于jQuery选择器,我们应该使用转义字符在方括号中包含方括号;
iterator = 0;
$('#field\\ ['+ iterator +'\\] \\ [a\\''')。val();
Let's say i have this form:
<form method="post" name="b" >
<table>
<tr>
<td>Field A</td>
<td><input type='text' name='field[0][a]' id='field[0][a]'></td>
<td>Field B</td>
<td><textarea name='field[0][b]' id='field[0][b]'></textarea></td>
<td><button>remove</button></td>
</tr>
</table>
</div>
<div id="container"></div>
<button id="mybutton">add form</button>
<div align="center">
<p><button onClick="dadosFormulario()" value="Registar" name="registar">Registo</button></p>
</div>
</form>
and i have a jquery function that duplicates the inputs i have when i click the "add form" button. So, regarding that, i'm trying to save the values of the inputs with the .val() method of jquery.
So, if i have an input like this one:
<input type='text' name='teste' id='teste'>
i know that with this:
var v = $('#teste').val();
i can receive her value. However, if i have an array of inputs, and that's my situation since i can duplicate the fields of the form, how can i obtain the multiple values from the following inputs?
<input type='text' name='field[0][a]' id='field[0][a]'>
<input type='text' name='field[0][b]' id='field[0][b]'>
var v = $('#field[iterator][a]').val(); won't work, it results in 'undefined'.
jquery function where i can't get the values from the inputs with the val(); gives undefined:
$countForms = 1;
(function($){
$.fn.addForms = function(idform){
var myform = "<table>"+
" <tr>"+
" <td>Field A ("+$countForms+"):</td>"+
" <td><input type='text' name='field\\["+$countForms+"\\]\\[a\\]'></td>"+
" <td>Field B ("+$countForms+"):</td>"+
" <td><textarea name='field["+$countForms+"]['b']'></textarea></td>"+
" <td><button>remove</button></td>"+
" </tr>"+
"</table>";
if(idform=='mybutton'){
myform = $(myform);
$("button", $(myform)).click(function(){ $(this).parent().parent().remove(); });
$(this).append(myform);
$countForms++;
}
};
})(jQuery);
$(function(){
$("#mybutton").bind("click", function(e){
e.preventDefault();
var idform=this.id;
if($countForms<3){
$("#container").addForms(idform);
}
});
});
Square brackets are used in jQuery selector and we should use escape characters to include the square bracket in id of control;
iterator = 0;
$('#field\\['+iterator +'\\]\\[a\\]').val();
这篇关于用jquery获取数组的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!