用jquery从表单中选择多维数组 [英] Select multidimensional arrays from form with jquery

查看:361
本文介绍了用jquery从表单中选择多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用JQuery选择一个多维表单项。但是当我试图提醒它的价值时,我只是得到一个 undefined



这是我的形式:

 < select name ='item [1] [name]'> 
< option value ='1'>名称1< / option>
< option value ='2'>名称2< / option>
< option value ='3'>名称3< / option>
< option value ='4'>名称4< / option>
< / select>

< input type ='text'name ='item [1] [id]'class ='text'/>

当我点击提交按钮时,我尝试选择所有表单元素。



我成功选择了文本输入字段:

  var item1i = $('input [名称= 项[1] [ID]]'); 
var personi = $(item1i).val();

但是当我尝试选择时,请选择标签是这样的:

  var item1n = $('input [name =item [1] [name]]') ; 
var personn = $(item1n).val();

当我提醒 personi 时,正确的值是所示。但当我提醒 personn 时,我得到 undefined



任何想法我做错了什么?

解决方案

使用 $('select [name =item或者, $(':input [name =']');

item [1] [name]]');



input will只选择输入标记的元素,因此对于您的选择,您必须使用选择标记。



或者,jquery提供了特殊的:input 选择器,它返回任何表单元素(输入文本,选择,复选框,textarea等),所以你也可以使用它。


I tried to select a multidimensional form item with JQuery. But when I try to alert its value I just get an undefined.

This is my form:

    <select name='item[1][name]'>
       <option value='1'>Name 1</option>
       <option value='2'>Name 2</option>
       <option value='3'>Name 3</option>
       <option value='4'>Name 4</option>
    </select>

    <input type='text' name='item[1][id]' class='text' />

When I click on submit button, I try to select all my form elements.

I successfully selected the text input field with:

var item1i = $('input[name="item[1][id]"]');
var personi = $(item1i).val();

But when I try to select the select tag like this:

var item1n = $('input[name="item[1][name]"]');
var personn = $(item1n).val();

When I alert personi the right value is shown. But when I alert personn I get undefined.

Any idea what I'm doing wrong?

解决方案

Use $('select[name="item[1][name]"]');

Or alternatively, $(':input[name="item[1][name]"]');

input will only select the elements with input tag, so for your select you have to use the select tag.

Alternatively jquery provides the special :input selector that returns any form element (input texts, selects, checkboxes, textarea,etc), so you can use that too.

这篇关于用jquery从表单中选择多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆