2014-08-27 138 views
0

我有一個實例,其中我的datepickers不會按按鈕工作,所以我找到最接近的輸入和.click()或.focus()(因爲它是datepicker,因爲相同的字段會受到影響)。爲了演示,從按鈕/圖標點擊 - 找到最接近的輸入並聚焦/點擊它。如果正確,邊框將呈藍色。 http://jsfiddle.net/81tvr0op/更改最接近的輸入值?

HTML

<table> 
    <tr> 
     <th> 
      <div class="input-group date"> 
       <input type="text" /> 
       <span class="input-group-btn"> 
        <button class="btn default" type="button"> 
         <i class="fa fa-calendar"></i> 
         Icon 
        </button> 
       </span> 
      </div> 
     </th>  
    </tr> 
</table> 

jQuery的

...像

$(".date > span > button").on('click', function (e) { 
       e.preventDefault(); 
       $(this).closest("th").find("input:text").click(); 
      }); 

...對不對?

回答

2

如果我理解正確,當用戶單擊按鈕時,您試圖將光標置於輸入框中。

要做到這一點,您應該使用.focus()而不是.click()

工作例如: http://jsfiddle.net/81tvr0op/1/

0

從你的jsfiddle,這樣的事情應該工作。

$("button").on('click', function (e) { 
    e.preventDefault(); 
    $(this).parent().siblings(0).focus(); 
}); 

或者,給你輸入自己的ID和做類似:

$("button").on('click', function (e) { 
    e.preventDefault(); 
    $("#dateInput").focus(); 
});