选择选项使用JQUERY通过文本获取价值 [英] Select option get value by text using JQUERY

查看:51
本文介绍了选择选项使用JQUERY通过文本获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多选择的HTML选择(下拉).我也有很多名字旁边带有按钮的表.我需要当您按任意名称旁边的按钮时,然后在选择框(下拉列表)中选择该名称.选项值是id,选项文本是name. 示例:

I have html select (dropdown) with many populated options. Also I have table with many Names with button next to it. I need that when you press the button next to any name then that name would be selected in select box(dropdown). Option value is id and option text is name. Example:

!NamesList!v!   <- select box

John  !Button!
Tom   !Button!
Laura !Button!    


我尝试使用:


I have tried to use:

var name = "John";
$("#cmbOperator option[value='" + name + "']").attr('selected', 'selected'); 

但是它不起作用.如果我将var名称更改为id(这是值),则可以工作.但是,如何从其文本中获取期权的价值呢?

but it doesn't work. If I change var name to id(which is value) then works. But how can I get option's value from its text?

推荐答案

$("select option:contains(text)").attr('selected', true);

这是JSFiddle

Here is the JSFiddle

http://jsfiddle.net/u1c21mq6/1/

这篇关于选择选项使用JQUERY通过文本获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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