是否可以在HTML中的选择标记下仅选择所选字段

时间:2014-11-17 18:53:36

标签: jquery

我有这个HTML

<div class="col76 pull-left">
   <select class="m-wrap" id="T1Select" tabindex="1" style="width:100%;">
      <option class="placeholder" selected="" disabled="">Select T1</option>
      <option value="Chips And Chocolates">Chips And Chocolates</option>
      <option value="Ice creams">Ice creams</option>
      <option value="One">One</option>
      <option value="Popcorn">Popcorn</option>
      <option value="Snacks And Corn">Snacks And Corn</option>
      <option value="Soft Drinks">Soft Drinks</option>
      <option value="Tea And Coffee">Tea And Coffee</option>
   </select>
</div>

我试过这种方式

  var valueone = One;
  var T1Val = $("#T1Select").val();

     if(valueone==T1Val)
         {
    $("#T1Select option").each(function(){
     if ($(this).val().toLowerCase() == valueone) {
    $(this).attr("disabled", "disabled");
    }
    });
    }

我怎样才能制作&#34; One&#34;可以选择动态选择标签吗?

http://jsfiddle.net/6kb7dhwq/10/

3 个答案:

答案 0 :(得分:1)

你几乎拥有它。您只需要您想要选择的值和一个循环来禁用其余的值: http://jsfiddle.net/lsubirana/bajy5ntc/

var valueone = "one";

$("#T1Select option").each(function(){
    if ($(this).val().toLowerCase() !== valueone) {
        $(this).attr("disabled", "disabled");
    }
});

答案 1 :(得分:0)

对于任何动态选择值的选择框,您只需传递所选框中的值即可 所以你应该这样做

 $("#T1Select").val("value of One or any element you want to select");

这对我有用

 <div class="col76 pull-left">
<select class="m-wrap" id="T1Select" tabindex="1" style="width:100%;">
  <option class="placeholder">Select T1</option>
  <option value="Chips And Chocolates">Chips And Chocolates</option>
  <option value="Ice creams">Ice creams</option>
  <option value="One">One</option>
  <option value="Popcorn">Popcorn</option>
  <option value="Snacks And Corn">Snacks And Corn</option>
  <option value="Soft Drinks">Soft Drinks</option>
  <option value="Tea And Coffee">Tea And Coffee</option>
 </select>
</div>

$("#T1Select").val("One");

希望这会有所帮助。

快乐学习:)

答案 2 :(得分:0)

试试这个

 var valueone = "one";

               $("#T1Select option").each(function(){
               if ($(this).val().toLowerCase() != valueone) {
                 $(this).attr("disabled", "disabled");
                }else{
                 $(this).removeAttr("disabled");
                }
               });