0

这是我遇到的一个非常小的问题。以下是我的 html 选择标签

<select id="category" name="category" style="opacity: 0; ">             
                <optgroup label="Default">  
                    <option value="Finance">Finance</option>
                    <option value="IT">IT</option>
                </optgroup>
                <optgroup label="User Created">             
                    <option value="Categoy">Categoy</option>
                    <option value="My Category">My Category</option>
                </optgroup>
      </select>

当用户选择用户创建标签 optgroup 中的选项时,我必须显示一个按钮。谁能给我一个建议如何用jquery做

谢谢

4

2 回答 2

5

小提琴:http: //jsfiddle.net/tariqulazam/27RLj/

HTML

<select id="category" name="category" style="opacity: 1; ">             
   <optgroup label="Default">  
         <option value="Finance">Finance</option>
         <option value="IT">IT</option>
   </optgroup>
   <optgroup label="User Created">             
         <option value="Categoy">Categoy</option>
         <option value="My Category">My Category</option>
   </optgroup>
</select>

<input type="button" value="button" id="btn"/>​

代码

$(document).ready(function(){
    $("#btn").hide();


    $("#category").change(function(){
        var selected = $("option:selected", this);
        selected.parent()[0].label=="User Created"?$("#btn").show(): $("#btn").hide();     

    });

});​
于 2012-10-25T04:07:40.803 回答
1

HTML

    <select id="category" name="category" style="opacity: 1; ">             
            <optgroup label="Default">  
                <option value="Finance">Finance</option>
                <option value="IT">IT</option>
            </optgroup>
            <optgroup label="User Created">             
                <option value="Categoy">Categoy</option>
                <option value="My Category">My Category</option>
            </optgroup>
  </select>
 <input id="btn" type="button" style="display:none" value="Press Me" />

脚本

    $(document).ready(function() {
    $("select").change(function() {
    var clicked = $(this)
                     .find('option:selected') // get selected option
                     .parent()   // get that option's optgroup
                     .attr("label");   // get optgroup's label
       if( clicked == "User Created")
       {
         $('#btn').css('display','');
        }
        else
        {
            $('#btn').css('display','none');
        }
    });
});

看看这个小提琴

于 2012-10-25T04:11:52.220 回答