如何获得<选择>带有jquery的id
How to get the <select> id with jquery
使用jquery脚本浏览HTML表单元素,我发现我可以使用以下代码获得标签的id:
$(":checkbox,:radio,:text,:selected", this).each(function(){
var controlName = $(this).attr("id");
var controlType = $(this).attr("type");
alert(controlName);
if(controlType=="checkbox" || controlType=="radio"){
if ($(this).attr('checked')=="checked"){cadena += "&" + controlName + "=on";}
else{ cadena += "&" + controlName + "=off";}
}
else{
cadena += "&" + controlName + "=" + $(this).attr('value');
}
});
它可以使用复选框、radioButtons和textInputs,为我提供正确的id。但对于select标记,它总是返回"undefined"。
我做错了什么?
谢谢。
*更新*这是我当前的表单。我知道这不是一个"正常"表单,但我正在一个特殊的应用程序中使用它。感谢大家的耐心:
<form action="#" id="ejemplo" class="ejemplo">
<fieldset>
<legend> Formulario de ejemplo </legend>
<input id="Nombre" type="text" value="Texto1" name="Nombre" /> <label for="Nombre">Nombre</label> <br/>
<input id="Apellidos" type="text" value="Texto1" name="Apellidos" /> <label for="Apellidos">Apellidos</label> <br/>
<label for="campo2">Campo 2</label>
<select id="coches">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<br/>
<input type="checkbox" id="option1"> <label for="option1"> Milk </label>
<input type="checkbox" id="option2"> <label for="option2"> Butter </label>
<input type="checkbox" id="option3"> <label for="option3"> Cheese </label>
<br/>
<input type="radio" name="group1" id="radio1"> <label for="radio1"> Milk </label> <br/>
<input type="radio" name="group1" id="radio2"> <label for="radio2"> Butter </label> <br/>
<input type="radio" name="group1" id="radio3"> <label for="radio3"> Cheese </label>
<br/>
<textarea cols="40" rows="5" id="areaTexto">Inside de text area!</textarea>
<br/>
<input id="boton" type="button" value="Boton"><br>
</fieldset>
</form>
获取select"coche"id后,返回"undefined"。但是,让它的值返回正确的值。
运行我的代码后,它返回:
&Nombre=文本1&Apellidos=文本1&undefined=volvo&option1=关闭&option2=关闭&option3=关闭&radio1=关闭&radio2=关闭&radio3=关闭
查看undefined=volvo:S
:选择的不是你想象的那样。
jQuery:选择
描述:选择所有选定的图元。
添加的版本:1.0
jQuery( ":selected" )
:选定的选择器适用于元素。它不适用于复选框或无线电输入;使用:已检查。
您希望使用select
来获取select元素。
或者,您可以使用:input
来获取所有输入元素。
:selected
匹配选定的<option>
元素,而不是<select>
元素。更改选择器:
$(":checkbox,:radio,:text,select", this)
N.B.$(this).attr('id')
总是可以替换为this.id
。
<select id="foo">
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
</select>
对于select标签,您可以使用:selected
过滤器来获取select选项的父项的id:
var id = $(this).children(":selected").attr("id");
相关文章:
- Jquery id 无法正常工作
- Jquery #id ul li 一个选择
- 如何动态地给出 jquery ID
- Jquery id 选择器变量无法绑定到 onchange
- jQuery ID 选择器仅适用于第一个元素
- Javascript/jQuery ID 检查以通过验证驱动编号功能
- jquery id 选择器的问题
- 使用 for 循环访问 jQuery ID
- 如何使用 PHP 获取 jQuery ID 值
- jQuery id 选择器在
- jquery id匹配但获胜'根本不起作用
- jQuery ID选择器在I'I’我正盯着大自然看
- Looping through JQuery Id's
- jQuery ID & function
- 为什么jQuery ID不适用于ajax中的下拉列表
- 如何为一堆php生成的DIV设置jQuery ID
- 如何从会话PHP中获取jQuery ID
- 在哪里放置jquery id点击函数
- JQuery id目标与一个变量
- 我如何在函数中使jquery id选择等于这个