2013-07-03 34 views
0

所以我試圖提交一個表單在jquery隱藏或顯示的東西。Jquery:表單提交不正常

但是在再次提交後它不起作用。

HTML:

<form id="filters" href="#"> 
     <table id="filter" class="table"> 
       <thead> 
        <tr> 
        <th>Show/Hide</th><th>Column</th> 
        </tr> 
       </thead> 
       <tbody> 
        <tr> 
        <td> 
        <input type="checkbox" name="checkNumber" value="1"/> 
        </td> 
        <td> 
        Account Number 
        </td> 
        </tr> 
       </tbody> 
     </table> 
</form> 

的Jquery:

$(function() { 
    $('#filters').submit(function() { 
     if ($('input[name=checkNumber]').attr('checked') == true) { 
      $('input[name=search_account]').show("normal");  
     } 
     else if ($('input[name=checkNumber]').attr('checked') !== true) { 
      $('input[name=search_account]').hide("normal"); 
     } 
      return false; 
    }); 

}); 

我試過很多其他方法,但它所有的工作原理相同。我提交,它隱藏了正確的輸入標籤,但是當我重新提交以顯示它時,它不起作用。

如果我通過選中複選框提交它也不起作用。

任何幫助將不勝感激!

+0

你還是試試'else',而不是'其他if'。 –

+0

再次看看你的代碼 - 你到底想要達到什麼目的?表單提交後,它將重新加載頁面,不是嗎? –

+0

不,它不重新加載頁面,它只是提交jquery來執行。 – designtocode

回答

0

剛剛意識到你的HTML是無效的爲好。沒有href屬性,你應該使用action來代替。但在你的情況下,你似乎根本不需要表格。

我想你可能想實現的是:

<html> 
    <head> 
     <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> 
    </head> 
    <body> 
     <table id="filter" class="table"> 
      <thead> 
       <tr> 
        <th>Show/Hide</th><th>Column</th> 
       </tr> 
      </thead> 
      <tbody> 
       <tr> 
        <td> 
         <input type="checkbox" name="checkNumber" value="1" /> 
        </td> 
        <td> 
         Account Number 
        </td> 
       </tr> 
      </tbody> 
     </table> 

     <input name="search_account" style="display: none;" /> 

     <script type="text/javascript"> 
      $(function() { 
       $("#filter input[name=checkNumber]").click(function() { 
        if ($(this).is(":checked")) { 
         $("input[name=search_account]").show(); 
        } else { 
         $("input[name=search_account]").hide(); 
        } 
       }); 
      }); 
     </script> 

    </body> 
</html> 
0

使用.on('submit',function(){ your code here and check your code in the else if part. you should use != not !==});

+0

不起作用,感謝您的幫助! – designtocode

0

試試這個。

else if ($('input[name=checkNumber]').attr('checked') != true) { 
      $('input[name=search_account]').hide("normal"); 
     } 
+0

不起作用,感謝您的幫助! – designtocode

0

我要說的是,你應該使用的

$('input[name=checkNumber]').is(':checked') 

代替

$('input[name=checkNumber]').attr('checked') 
+0

試過這個,不起作用。我正在使用這個庫,它是否有所作爲? designtocode