开发者

Jquery not method problem with button tag

i don't why but the not() method of jquery return the same value for two differents tests that are exclusive.

Look:

<script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>

<script language="javascript">
    $(function() {
        $(':button, :submit').filter(function() {
            $('#result').append('tag = ' + $(this)[0].tagName + "<br />");
            $('#result').append('.not(\':submit\').length = ' + $(this).not(':submit').length + "<br />");
            $('#result').append('.not(\':button\').length = ' + $(this).not(':button').length + "<br />");
            $('#result').append('.is(\':button\') = ' + $(this).is(':button') + "<br /><br />");

        });

    });

</script>

<button>Btn1</button>
<input type="button" value="Btn2" />
<input type="submit" />

<div id="result">
</div>

Here are the results :

tag = BUTTON

.not(':submit').length = 0

.not(':button').length = 0

.is(':button') = true

tag =开发者_StackOverflow社区 INPUT

.not(':submit').length = 1

.not(':button').length = 0

.is(':button') = true

tag = INPUT

.not(':submit').length = 0

.not(':button').length = 1

.is(':button') = false

Found a bug ?


No, this is by design. For the specific case of the button element, both the :submit filter and the :button filter are true. button elements have the submit type by default (see the MDN docs).


I assume you are confused by

.not(':submit').length = 0
.not(':button').length = 0

Your assumption is wrong, your tests are not exclusive. The jQuery documentation states:

:button: Selects all button elements and elements of type button.
:submit: Selects all elements of type submit.

Now, it happens to be that button elements have a type attribute and the default value is submit.

type = submit|button|reset [CI]
This attribute declares the type of the button. Possible values:

  • submit: Creates a submit button. This is the default value.
  • reset: Creates a reset button.
  • button: Creates a push button.

So jQuery returns the correct results. not(:button).length returns 0 because we are dealing with a button elements. not(:submit).length returns 0 because that button element as a type attribute with value submit.


Change filter(function() {}) to each(function(){}) then you may be in luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜