Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 根据其文本而非值选择下拉选项_Jquery - Fatal编程技术网

Jquery 根据其文本而非值选择下拉选项

Jquery 根据其文本而非值选择下拉选项,jquery,Jquery,我试图通过文本选择下拉列表项。我不能按值选择它们,因为每次值都不同 $("#rn_SelectionInput_7_attend_session_time option").each( function(){ alert($(this).text()==radioValue);// this evaluate to true when a matching item is found if( $(this).text()==radioValue) {

我试图通过文本选择下拉列表项。我不能按值选择它们,因为每次值都不同

   $("#rn_SelectionInput_7_attend_session_time option").each( function(){
      alert($(this).text()==radioValue);// this evaluate to true when a matching item is found

     if( $(this).text()==radioValue) 
     {
        $("#rn_SelectionInput_7_attend_session_time").selectedIndex=$(this.index()); // this doesn't do anything
     }
   });

谢谢,

您可以将option元素的selected属性设置为selected

$("#rn_SelectionInput_7_attend_session_time option").filter(function () {
    alert($(this).text() == radioValue); // this evaluate to true when a matching item is found
    return $.trim($(this).text()) == radioValue
}).prop('selected', true);
演示:

  • -查找具有给定文本的选项元素
  • -将所选属性设置为true

谢谢您提供的代码。但只有当我将道具更改为属性时,它才起作用。有什么想法吗?这是特定于浏览器的吗?@BishnuPaudel这取决于使用的jQuery版本。。。哪一个是jQuery版本?如果不是特定于浏览器的,那么usedit是好的。我现在工作的页面使用1.4.2,我无法更改。谢谢,@BishnuPaudel如果您使用的是jQuery 1.4.2,那么您需要使用
.attr()
,如果jQuery>=1.6.4,那么
.prop()