简体   繁体   English

我可以从给定的选择框文本中获取选择框值

[英]Can I get Select box value from given select box text

Can I get Select box value from given select box text ? 我可以从给定的选择框文本中获取选择框值吗? Need help 需要帮忙

<select id='list'>
<option value='1'>Option A</option>
<option value='2'>Option B</option>
<option value='3'>Option C</option>
</select>

$("#list[text='Option B']").val();

Try 尝试

$("#list option:contains(Option B)").val();

Edit, Updated 编辑,更新

As pointed out by @dfsq , commenting on original answer 正如@dfsq所指出的,评论原始答案

Not very reliable, as it will also much "Option Boom". 不太可靠,因为它也会有很多“选择热潮”。 filter might be better option. 过滤器可能是更好的选择。

tests http://jsfiddle.net/5cy9tt2o/1/ 测试http://jsfiddle.net/5cy9tt2o/1/

Additionally, 另外,

because :contains selector just checks indexOf for occurrences. 因为:contains选择器只检查indexOf的出现次数。 See the source of it here . 这里查看它的来源。 ... usually it's not a problem, just > something to be aware of. ...通常这不是问题,只是>需要注意的事情。

where " :contains selector just checks indexOf for occurrences" does not appear at documentation ? :contains选择器只检查indexOf是否出现”文档中没有出现?


Utilizing .filter() may also return "unreliable" results if a particular match is found, or not found , by the provided match to search for within the given element text. 如果通过提供的匹配找到或找不到特定匹配,则使用.filter()也可能返回“不可靠”结果,以在给定元素文本内搜索。

For example, @dfsq provided the example of 例如,@ dfsq提供了示例

$("#list option:contains(Option B)").val();

matching 匹配

"Option Boom"

The accepted answer , though utilizing .filter() may not return expected results if " " appeared after text 如果在text后面出现" " ,则接受的答案虽然使用.filter()可能不会返回预期的结果

<option value='2'>Option B </option>

tests http://jsfiddle.net/5cy9tt2o/4/ 测试http://jsfiddle.net/5cy9tt2o/4/


An alternative approach, which should be expected to return exact match , without calling addition internal methods ; 一种替代方法,应该预期返回完全匹配,而不需要调用额外的内部方法; though passing RegExp parameters, or any other settings , could , of course, be optional. 虽然传递RegExp参数或任何其他设置当然可以是可选的。

Basic and primary focus of method to return exact match, if that is possible, of text passed to the method. 方法的基本和主要焦点,如果可能的话,返回传递给方法的文本的完全匹配。

Given html from original post, modified with same test conditions tried at above, 鉴于来自原始帖子的html ,在上面尝试过相同的测试条件进行修改,

<select id='list'>
<option value='1'>Option Boom</option>
<option value='2'>Option B</option>
<option value='3'>Option C </option>
</select>

js JS

(function($) {
  $.fn.tryToMatchExactText = function (text) {
    return $("*").filter(function(i, el) {
      return el.textContent.match(new RegExp("^"+text+"$")) !== null
    })
  };
}(jQuery));

var list = $("#list option");

console.log(list.tryToMatchExactText("Option B").val() // `"2"`
       , list.tryToMatchExactText("Option Boom").val() // `"1"`
       , list.tryToMatchExactText("Option C").val() // `undefined`
       , list.tryToMatchExactText("Option C ").val() // `"3"`
);

tests http://jsfiddle.net/5cy9tt2o/3/ 测试http://jsfiddle.net/5cy9tt2o/3/

You can use filter: 你可以使用过滤器:

$("#list option").filter(function(){
     return $(this).text().trim() == 'Option B';
}).val();

You could also use $("#list option:contains(Option B)") , but it would bring multiple results in your case. 您也可以使用$("#list option:contains(Option B)") ,但它会在您的情况下带来多个结果。

去做 :

$("#list").children().eq(2).val();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM