0

So I'm trying to submit a form in jquery to hide or show stuff.

But it doesn't work after submitting again.

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;
    }); 

});

I've tried so many other ways, but it all works the same. I submit, it hides the right input tag but when I resubmit to show it, it doesn't work.

It also doesn't work if I submit it with the checkbox checked.

Any help will be appreciated!

4

4 回答 4

0

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

于 2013-07-03T08:33:58.463 回答
0

尝试这个。

 else if ($('input[name=checkNumber]').attr('checked') != true) {
           $('input[name=search_account]').hide("normal");
       }
于 2013-07-03T08:34:16.233 回答
0

我会说你应该使用

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

代替

$('input[name=checkNumber]').attr('checked')
于 2013-07-03T08:49:46.613 回答
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>
于 2013-07-05T12:28:38.873 回答