怎样得到select所有option里的值

怎样得到select所有option里的值,第1张

$("option")map(function(){return $(this)val();})get()join(", ")

代码说明:

使用map()函数把每个option的值传递到当前匹配集合,生成包含返回值的对象;

使用 get() 处理返回的对象以得到基础的数组;

使用join()函数组装字符串。

实例演示如下:

1、HTML结构

<select id="test">

<option value="option-1">option-1</option>

<option value="option-2">option-2</option>

<option value="option-3">option-3</option>

</select><br>

<input type="button" value="获取所有option值">

jQuery是控制和 *** 作select详解。

先看下面的html代码

<select id="test">

<option value="1">选项一<option>

<option value="2">选项一<option>

<option value="n">选项N<option>

</select>

所谓jQuery *** 作“select”, 说的更确切一些是应该是jQuery控制 “option”, 看下面的jQuery代码:

//获取第一个option的值

$('#test option:first')val();

//最后一个option的值

$('#test option:last')val();

//获取第二个option的值

$('#test option:eq(1)')val();

//获取选中的值

$('#test')val();

$('#test option:selected')val();

//设置值为2的option为选中状态

$('#test')attr('value','2');

//设置最后一个option为选中

$('#test option:last')attr('selected','selected');

$("#test")attr('value' , $('#test option:last')val());

$("#test")attr('value' , $('#test option')eq($('#test option')length - 1)val());

//获取select的长度

$('#test option')length;

//添加一个option

$("#test")append("<option value='n+1'>第N+1项</option>");

$("<option value='n+1'>第N+1项</option>")appendTo("#test");

//添除选中项

$('#test option:selected')remove();

//删除项选中(这里删除第一项)

$('#test option:first')remove();、

//指定值被删除

$('#test option')each(function(){

if( $(this)val() == '5'){

$(this)remove();

}

});

$('#test option[value=5]')remove();

//获取第一个Group的标签

$('#test optgroup:eq(0)')attr('label');

//获取第二group下面第一个option的值

$('#test optgroup:eq(1) : option:eq(0)')val();

jQuery获取Select元素,并设置的 Text和Value:

$("#select_id ")get(0)selectedIndex=1; //设置Select索引值为1的项选中

$("#select_id ")val(4); // 设置Select的Value值为4的项选中

$("#select_id option[text='jQuery']")attr("selected", true); //设置Select的Text值为jQuery的项选中

jQuery添加/删除Select元素的Option项:

$("#select_id")append("<option value='Value'>Text</option>"); //为Select追加一个Option(下拉项)

$("#select_id")prepend("<option value='0'>请选择</option>"); //为Select插入一个Option(第一个位置)

$("#select_id option:last")remove(); //删除Select中索引值最大Option(最后一个)

$("#select_id option[index='0']")remove(); //删除Select中索引值为0的Option(第一个)

$("#select_id option[value='3']")remove(); //删除Select中Value='3'的Option

$("#select_id option[text='4']")remove(); //删除Select中Text='4'的Option

三级分类 <select name="thirdLevel" id="thirdLevel"

onchange="getFourthLevel()">

<option value="0" id="thirdOption">

请选择三级分类

</option>

</select>

</div>

四级分类:

<select name="fourthLevelId" id="fourthLevelId">

<option value="0" id="fourthOption">

请选择四级分类

</option>

</select>

</div>

if($("#thirdLevel")val()!=0){

$("#thirdLevel option[value!=0]")remove();

}

if($("#fourthLevelId")val()!=0){

$("#fourthLevelId option[value!=0]")remove();

}//这个表示:假如希望当选择选择第三类时:如果第四类中有数据则删除,如果没有数据第四类的商品中的为默认值。

获取Select :

获取select 选中的 text :

$("#ddlRegType")find("option:selected")text();

获取select选中的 value:

$("#ddlRegType ")val();

获取select选中的索引:

$("#ddlRegType ")get(0)selectedIndex;

设置select:

设置select 选中的索引:

$("#ddlRegType ")get(0)selectedIndex=index;//index为索引值

有时候我们需要在一个select中传递两个有关联但不同的值,比如你的这个情况。这时候要利用到option的另一个不常用属性name,用name来存储第二个值,然后在onchange事件中分别用thisoptions[thisselectedindex]name来调用这个值。

全12333

全12321

全123123

在你的程序里,把

之间的内容作为name属性写出来,然后定义一个隐藏的input——optt,再定义好onchange事件,就可以同时获取到value和name两个值了。

今天写代码遇到一个问题,在jsp页面中通过form的得到的select标签的值,form提交给自身页面,然后通过requestgetParameter()方法取得值测试代码如下(文件名为:testselectjsp): My JSP 'testselectjsp' starting page 通过requestgetParameter("number")方法取得下拉框选取的值 运行界面:

我也想知道,取到的值,如何调用,我是想验证一下取的值里有没有我要的值,想在SQL语句外加个 if 判断,这个select是在函数里用的,因为没得到相应的结果,不知道是哪儿出的问题,想用if判断一下这个值取到没有,如何处理,谢谢!代码如下(---后面是个判断,不知对否):

set HyTRs=nothing

set HyTRs=serverCreateObject("adodbrecordset")

sql = "select from HyClub where KeyString like '" & KeyString & "%' and KeyString<>'"&KeyString&"' and IsApproved=true"

HyTRsOpen sql,conn

if HyTRsRecordCount>0 then '如果有此number

Do While Not HyTRseof

ind = len(HyTRs("KeyString")) - len(KeyString)

if ind <= 5 then

user = CFANDPD(HyTRs("HyNumber"))‘----调用一个函数

‘------------------------

if HyNumber="CF60016811" then

responsewrite("---------222222--------无痕秋水"&user&"长度差"&ind)

end if

请指教,谢谢!

已经搞定,我写的还是对的:)

楼主可以用我这个方法 HyNumber就是结果集中的,直接用字段名就可以调用!

以上就是关于怎样得到select所有option里的值全部的内容,包括:怎样得到select所有option里的值、jquery怎么获得select选中的值、如何获取select 里option的值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9809323.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存