如何点击按钮打开下拉列表?

时间:2015-07-13 05:14:14

标签: javascript jquery html ruby-on-rails-3 ruby-on-rails-4

我的html页面上有一个下拉列表,该页面上有一个按钮。 现在我想做一些像用户点击按钮然后下拉列表那样的东西。

按钮: -

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

下拉: -

<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>

我试过这个jquery代码

$('.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);
    } 
}

但这只会工作一次。如果我再次点击按钮,则下拉列表不会打开。 什么&#39;出错了?

提前感谢。

3 个答案:

答案 0 :(得分: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.com/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>

答案 1 :(得分:1)

按钮 下拉列表 放在 两个不同的div标签< / em> 如下所示 ids

12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2) java.lang.NullPointerException

12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2)  at org.docx4j.jaxb.NamespacePrefixMapperUtils.setProperty(NamespacePrefixMapperUtils.java:155)

12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2)  at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:588)

12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2)  at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:559)

12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2)  at org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart.createVirtualStylesForDocDefaults(StyleDefinitionsPart.java:369)

12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2)  at org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.getStyleTree(MainDocumentPart.java:172)

12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2)  at org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.getStyleTree(MainDocumentPart.java:161)

12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2)  at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.stylesToCSS(XHTMLImporterImpl.java:446)

12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2)  at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.getRenderer(XHTMLImporterImpl.java:253)

12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2)  at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.convert(XHTMLImporterImpl.java:645)

然后使用以下脚本代码

<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>

Example Fiddle

答案 2 :(得分:0)

这对我有用。

TimerTask

我使用了超时,因为当我再次点击按钮时,mousedown事件将不会触发,因为它有2个事件,所以我延迟了一个事件。它完全符合我的要求。