2014-12-08 36 views
0

我有類似以下HTML文件更好的選擇,寫這個jQuery

<i class=‘icon-calendar icon-large' data-placement='top' data-toggle='popover' type='button'> 
    <div class='head hide'>Calendar</div> 
    <div class='content hide'> 
     <form action="#" data-remote="true" id="calendar_form" method="post"> 
     <div style="..."> 
      <input type="hidden" /> 
      <input type="hidden" /> 
     </div> 
      <table border='0px'> 
      <tbody> 
       <tr> 
       <td colspan='2'> 
        <button class='btn btn-default btn-block' id='submit' type='submit'>Convert</button> 
       </td> 
      </tbody> 
      </table> 
     </form> 
     </div> 
     </i> 

<li class="..." id="calendar_example"><label class=" label" for="...">...</label> 
    <input class="..." id="calendar_example_1" name="[calendar_example][0][calendar_example_1][0]" type="text" /> 

我試圖使用jQuery寫的功能,當點擊按鈕其中ID是提交,我可以通過值從酥料餅與ID calendar_example_1,我的代碼,工作一個找到域的域,是

$(function(){ 
    $('body').on('click','#submit', function(){ 
    var calendar_name = $(this).parent().parent().parent().parent().parent().parent().siblings().find('#calendar_example_1').attr('name'); 
    return false; 
    }); 
}); 

通過.parent()多次,我不認爲這是寫的好辦法這段代碼。由於我是jquery的新手,我找不到任何更好的解決方案,但它以我想要的方式工作。任何人都可以給我一些建議?

編輯: 外部形式嵌套。

回答

2

你可以做到這一點,而不是:

var calendar_name = $(this).closest("i.icon-calendar").siblings("li#calendar_example").find('#calendar_example_1').attr('name'); 
+0

只是給了一個嘗試。不工作:。(... – user2775888 2014-12-08 06:47:32

+0

,但我們試圖用這種類似的邏輯運作良好,感謝 – user2775888 2014-12-08 06:56:56

2

您可以使用

$('#submit').click(function(){ 
var calendar_name = $('#calendar_example_1').attr('name'); 
}); 
+0

奇怪的是,我們已經用類似的方式,你試過了,沒有在所有的工作只需$(「#提交」 ).click(function(){,popover無法顯示。 – user2775888 2014-12-08 06:47:09