js或者jquery如何获取select中未选中的值急求

js或者jquery如何获取select中未选中的值急求,第1张

html:

<select

id="sel">

<option

value='s1'>苹果</option>

<option

value='s2'>西瓜</option>

<option

value='s3'>香蕉</option>

</select>

javascript:

$(function(){

var

_val

=

$map(

$("#sel

option:not(:selected)"),

function(ele){return

elevalue}

)join(",");

alert(_val);

})

其中主要的是:$("#sel

option:not(:selected)"),这是返回没被选中的option集合,

使用$map函数对这个集合进行处理,取出其中元素的值,使用","进行分隔。

如果option中没有value属性,那么直接返回option的文本内容。

1html代码如下:

<html>

<head>

</head>

<body>

<form name="form1" id="form1">

Select your favorite fruit:

<select id="mySelect" name="mySelect">

<option value="11">Apple</option>

<option value="22" >Orange</option>

<option value="33">Pineapple</option>

<option value="44">Banana</option>

</select>

<br /><br />

<input type="button" onclick="getIndex()"

value="Alert index of selected option">

</form>

</body>

</html>

2javascript代码如下:

<script type="text/javascript">

function getIndex()

{

//从document对象中,获取select标签

var a=documentgetElementById("mySelect");

//select标签获取的值其实是一个数组--aoptions[]; 然后,选定项的下标是--aselectedIndex

var b=aoptions[aselectedIndex]value;

alert(b);

}

</script>

var obj = documentgetElementById("n1"); // 这里也可以写成var obj = documentgetElementByName("n1");var arrText = new Array();var arrValue = new Array();

for(var i = 0; i < objoptionslength; i++) {

arrText [arrText length] = objoptions[i]text; arrValue[arrValuelength] = objoptions[i]value;

}arrText 就是所有的TextarrValue就是所有的Value

纯JS

var e = documentgetElementById("form-field-select-4");

alert(getSelectValues(e));

// Return an array of the selected opion values

// select is an HTML select element

function getSelectValues(select) {

  var result = [];

  var options = select && selectoptions;

  var opt;

  for (var i=0, iLen=optionslength; i<iLen; i++) {

    opt = options[i];

    if (optselected) {

      resultpush(optvalue || opttext);

    }

  }

  return result;

}

JQuery

 var selectedValues = [];    

 $("#form-field-select-4 :selected")each(function(){

     selectedValuespush($(this)val()); 

 });

 alert(selectedValues);

以上就是关于js或者jquery如何获取select中未选中的值急求全部的内容,包括:js或者jquery如何获取select中未选中的值急求、如何利用javascript获取表单中select下拉列表中所选中项的值value、通过js来获取select的全部值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址: http://outofmemory.cn/web/9623915.html

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

发表评论

登录后才能评论

评论列表(0条)

保存