开发者

jQuery form input count different in IE

I'm having a strange issue with counting form elements in jQuery. While I can work around it, I w开发者_如何学Cas wondering if anyone knows why there is a difference between browsers with the following example?

<!DOCTYPE html 
 PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>IE Test</title>
    <meta name="description" content="" />
    <meta name="keywords" content="" />
    <meta name="author" content="" />
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function(){
            alert($('form :input').length);
        });
    </script>
</head>
<body>      
    <div id='wrapper'>
        <div id='header'>
            <form action='#' method='POST'>
                <input type='text' name='title' value='Hotdog Fanatic'></input>
            </form>
        </div>
    </div>

</body>

IE 6/7/8 all give me a result of 2, while FF, chrome, opera and safari all count 1 matched element.

If I change the selector to filter by any attribute, the count appears correct. For example, form :input[name] or form :input[type] as the selector returns just one matched element in IE.

Does anyone know why this might be?

Thanks!

Baps.


Remove the </input> end tag.
Instead, you should make a self-closing tag:

<input type='text' name='title' value='Hotdog Fanatic' />

Change your script to

<script type="text/javascript">
    $(document).ready(function(){
        alert($('form :input')[1].outerHTML);
    });
</script>

This will alert </INPUT>.


Input elements are not containers, when using XHTML strict, as you are, replace it with this:

<input type='text' name='title' value='Hotdog Fanatic' />
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜