最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to open dropdown on click of button? - Stack Overflow

programmeradmin3浏览0评论

I have one dropdown on my html page and one button on that page. now I want to do something like thst when user click on button then dropdown will open.

Button:-

 <a href="javascript:void(0);" id="84" class="add_award_ready"><i class="fa fa-plus"></i></a>

Dropdown:-

<select id="instructor_student_award_id" name="instructor_student_award[id]">
 <option value="">Select</option>
 <option value="21">ss1</option>
 <option value="25">E++</option>
 <option value="26">F++</option>
 <option value="27">t1</option>
</select>

I tried this jquery code

$('.add_award_ready').click(function(){
  student_id = $(this).attr('id');
  var element = $('#add_award_form_' + student_id + ' select')[0], worked = false;
    if (document.createEvent) {
        var e = document.createEvent("MouseEvents");
        e.initMouseEvent("mousedown", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        worked = element.dispatchEvent(e);
    } 
}

but this will works only once.If I clicked once again on button then dropdown does not opens. whats' going wrong?

thanks in advance.

I have one dropdown on my html page and one button on that page. now I want to do something like thst when user click on button then dropdown will open.

Button:-

 <a href="javascript:void(0);" id="84" class="add_award_ready"><i class="fa fa-plus"></i></a>

Dropdown:-

<select id="instructor_student_award_id" name="instructor_student_award[id]">
 <option value="">Select</option>
 <option value="21">ss1</option>
 <option value="25">E++</option>
 <option value="26">F++</option>
 <option value="27">t1</option>
</select>

I tried this jquery code

$('.add_award_ready').click(function(){
  student_id = $(this).attr('id');
  var element = $('#add_award_form_' + student_id + ' select')[0], worked = false;
    if (document.createEvent) {
        var e = document.createEvent("MouseEvents");
        e.initMouseEvent("mousedown", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        worked = element.dispatchEvent(e);
    } 
}

but this will works only once.If I clicked once again on button then dropdown does not opens. whats' going wrong?

thanks in advance.

Share Improve this question edited Jul 13, 2015 at 10:46 sank asked Jul 13, 2015 at 5:14 sanksank 1,0143 gold badges13 silver badges25 bronze badges 4
  • jsfiddle/k667L5ns/2 – Umesh Sehta Commented Jul 13, 2015 at 5:26
  • I think you can check this <stackoverflow./questions/249192/…> – George Lica Commented Jul 13, 2015 at 5:33
  • didn't work for me. @GeorgeLica – sank Commented Jul 13, 2015 at 5:49
  • Can you post your dropdown and button code? – Pavan Commented Jul 13, 2015 at 7:12
Add a ment  | 

3 Answers 3

Reset to default 1
        <div style="text-align: right;float:right;margin: 0;">
                <a id="OpenDialog" href="javascript:void(0)" onclick="aopen()" > <i class="fa fa-plus"></i> Add Policy</a>                        
              </div>
              <script>
    function aopen()
    {
        $("#dialog").dialog({modal: true, height: 500, width: 800 });
    }
    function close_popup()
    {
        //alert('HEllo');
        $("#dialog").dialog('close');            
    }
    </script>


    <script src="//code.jquery./ui/1.11.4/jquery-ui.js"></script>

    <div id="dialog" title="Add Policy" style="display: none;" >
        <div class="box box-primary" >
            <!-- form start -->
            <form role="form" action="<?php echo site_url('policy/submit_data');?>" method="post">
              <div class="box-body">
                <div class="form-group">
                  <label for="exampleInputEmail1">Category Name</label>
                  <select class="form-control" name="category">
                        <option value=""> -- Category --</option>
                        <?php foreach($category as $value) {?>
                        <option value="<?php echo $value->cate_name; ?>"><?php echo $value->cate_name; ?></option> 
                        <?php } ?>
                  </select>

                </div>
                <div class="form-group">
                  <label for="exampleInputPassword1">Sub Category</label>
                  <select class="form-control" name="sub_category">
                        <option value=""> -- SUB category --</option>
                        <?php foreach($sub_cate as $value) {?>
                        <option value="<?php echo $value->cate_id; ?>"><?php echo $value->sub_cate; ?></option> 
                        <?php } ?>
                  </select>
                </div>
                <div class="form-group">
                  <label for="exampleInputPassword1">Description</label>
                  <div class='box-body pad'>
                        <textarea class="textarea" id="desc" name="description"  placeholder="Place some text here" style="width: 100%; height: 200px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;"></textarea>                          
                    </div>
                  </div>
                </div>                    
              </div>
              <!-- /.box-body -->
              <div class="box-footer">
                <button type="submit" class="btn btn-primary">Submit</button>
                <button type="button" onclick="return close_popup();" class="btn btn-primary">Cancel</button>
              </div>
            </form>
          </div>
    </div>

Put the button and drop-down inside two different div tags like below provided with ids.

<div id="myButton">
<a href="javascript:void(0);" id="84" class="add_award_ready"><i class="fa fa-plus"></i></a>
</div>

<div id="mySelect">
<select id="instructor_student_award_id" name="instructor_student_award[id]">
 <option value="">Select</option>
 <option value="21">ss1</option>
 <option value="25">E++</option>
 <option value="26">F++</option>
 <option value="27">t1</option>
</select>
</div>

Then use the below script code

$("#mySelect").hide();
$("#myButton").click(function(e){
   $("#mySelect").toggle('slow');//or just show instead of toggle
});

Example Fiddle

this is work for me.

setTimeout(function(){
 var element = $('#add_award_form_' + student_id + ' select')[0], worked = false;
 if (document.createEvent) {
  var e = document.createEvent("MouseEvents");
  e.initMouseEvent("mousedown", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  worked = element.dispatchEvent(e);
 } 
},100);

I used timeout because when I click on button again then mousedown event will not fire because it got 2 events so I delay one event. And its working exactly as I want.

发布评论

评论列表(0)

  1. 暂无评论