2013-12-22 52 views
0

我正在使用twitter引导前端。我有一个文本字段和一个下拉菜单旁边:Twitter下拉式引导设置字段

<div class="input-group"> 
    <input type="text" class="form-control" name="opening_hr" id="opening_hr" required> 

     <div class="input-group-btn"> 
     <button type="button" class="btn btn-default dropdown-toggle" 
     data-toggle="dropdown"><span class="caret"></span></button> 
     <ul class="dropdown-menu pull-right" id="opening_hr_select"> 

     @foreach($hours as $hour) 
     <li><a>{{$hour}}</a></li> 
     @endforeach 

    </ul> 
</div> 

我试着创建了一个JavaScript函数,它接受两个参数的文本字段我想编辑和下拉菜单中已选择:

$(function(){ 

    $(".dropdown-menu li a").click(function(){ 

    var elem = document.getElementById("opening_hr"); 

    elem.text($(this).text()); 
    elem.val($(this).text()); 
}); 

}); 

以上是简单的函数没有参数我试过测试(没有运气)。我想实现的是取出选定的值并将其设置为文本字段值。

请帮忙,谢谢。

回答

0

找到了一种简单的方法来实现我想要做的事情,而无需使用jQuery或JavaScript。以下是我的解决方案:

<!-- Opening times --> 
    <div class="row"> 
     <div class="col-lg-3"> 
      <label for="opening_time">Opening Time</label> 

       <div class="input-group"> 
        <span class="input-group-addon">Hour</span> 
        <input type="number" max="24" min="1" class="form-control" name="opening_hr" 
             id="opening_hr" 
             required> 

        <span class="input-group-addon">Minute</span> 
        <input type="number" max="60" min="0" class="form-control" name="opening_min" 
             id="opening_min" 
             required> 
        </div> 
     </div> 
     </div> 
1

的问题是,可变ELEM没有那么试图调用VAL将无法​​正常工作jQuery对象。因为你已经使用jQuery的ELEM分配更改为:

var elem = $("#opening_hr"); 

,并设置输入使用:

​​

或将它们组合在一起:

$("#opening_hr").val($(this).text()) 
0

我建议深入挖掘Jquery。其主要特点之一是支持选择器,所以在你使用Jquery的时候,你应该使用它来访问一个DOM元素,而不是使用getelementbyid。

ps:我的ipad不喜欢首都,对不起。