js 调用函数获取<select>标签值并输出到页面时出现undefined

js 调用函数获取<select>标签值并输出到页面时出现undefined,第1张

应该是你把

var index=documentgetElementById("count")selectedIndex;

var count=documentgetElementById("count")options[0]value;

这两句写在了文档的上方吧?也就是说这个select都还没加载进来的时候你就执行了这两条语句,自然就找不到这个标签了。

html里代码的执行是一遍编译一边执行,所以你把取值语句放在上面,赋值语句放在下面,那么它编译完取值语句时就直接执行了,但这时候赋值语句都还没有,自然就找不到这个值。你把它写在方法里之后,如果你调用方法的地方仍然是在select标签上面,那还是会提示找不到的。如果你是在它后面调用那就没有问题。

首先设置下拉列表控件的id属性

<select id="test" name="">

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

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

</select>

1:拿到select对象: var myselect=documentgetElementById("test");

2:拿到选中项的索引:var index=myselectselectedIndex ; // selectedIndex代表的是你所选中项的index。

3:拿到选中项options的value: myselectoptions[index]value;

4:拿到选中项options的text: myselectoptions[index]text;

另外还有jquery的方法(前提是已经加载了jquery库):

1:var options=$("#test option:selected"); //获取选中的项

2:alert(optionsval()); //拿到选中项的值

3:alert(optionstext()); //拿到选中项的文本

现在有一id=test的下拉框,怎么拿到选中的那个值呢?

分别使用javascript原生的方法和jquery方法

<select id="test"  name="">   

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

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

 </select>

code:

一:javascript原生的方法

  1:拿到select对象: var  myselect=documentgetElementById("test");

  2:拿到选中项的索引:var index=myselectselectedIndex ;             // selectedIndex代表的是你所选中项的index

  3:拿到选中项options的value:  myselectoptions[index]value;

  4:拿到选中项options的text:  myselectoptions[index]text;

二:jquery方法(前提是已经加载了jquery库)

1:var options=$("#test option:selected");  //获取选中的项

2:alert(optionsval());   //拿到选中项的值

3:alert(optionstext());   //拿到选中项的文本

var selectIndex = documentgetElementById("sect")selectedIndex;//获得是第几个被选中了

var selectText = documentgetElementById("sect")options[selectIndex]text //获得被选中的项目的文本

>

1js

var

obj=documentgetElementById(selectid);

objoptionslength

=

0;

//清除所有内容

objoptions[index]

=

new

Option("three",3);

//更改对应的值

objoptions[index]selected

=

true;

//保持选中状态

objadd(new

Option("4","4"));

”文本",”值"

var

index

=

objselectedIndex;objoptionsremove(index);//删除选中项

2jquery

$("#select_id")append("<option

value='Value'>Text</option>");

//为Select追加一个Option(下拉项)

$("#select_id")")find('option:selected')text();

获取select选中的text

$("#select_id")val();

获取select选中的value

$("#select_id

option[index='0']")remove();//删除索引值为0的Option

$("#select_id

option[value='3']")remove();

//删除值为3的Option

$("#select_id

option[text='4']")remove();

//删除TEXT值为4的Option

$("#mselect_id")change(function(){

//添加所需要执行的 *** 作代码

})

补充:

js获取select标签选中的值

var

obj

=

documentgetElementByIdx_x(”testSelect”);

//定位id

var

index

=

objselectedIndex;

//

选中索引

var

text

=

objoptions[index]text;

//

选中文本

var

value

=

objoptions[index]value;

//

选中值

jQuery中获得选中select值

第一种方式

$('#testSelect

option:selected')text();//选中的文本

$('#testSelect

option:selected')

val();//选中的值

$("#testSelect

")get(0)selectedIndex;//索引

第二种方式

$("#tesetSelect")find("option:selected")text();//选中的文本

……val();

……get(0)selectedIndex;

以上内容是小编给大家介绍的JS、jQuery中select的用法详解,希望对大家有所帮助!

以上就是关于js 调用函数获取<select>标签值并输出到页面时出现undefined全部的内容,包括:js 调用函数获取<select>标签值并输出到页面时出现undefined、js中怎么获得本页下拉菜单选定的值、如何使用js获取select中选中的option的value值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存