开发者

Attribute Starts With Selector [name^=value]

开发者 https://www.devze.com 2023-01-11 13:15 出处:网络
I need to match some input fields by name. The name consist of a base name plus squares brackets, so the PHP interpreter converts them to array. According to the jQuery API, I could use the following

I need to match some input fields by name. The name consist of a base name plus squares brackets, so the PHP interpreter converts them to array. According to the jQuery API, I could use the following selector:

":input[name^=foo\\[]"

(It's okay to ignore the closing bracket.) However, it won't match any of the elements. Other variations trigger an exception that I can't manage to catch properly. What am I missing?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html&开发者_高级运维gt;
<head><title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript"><!--
jQuery(function($){
    // Expected result: 4
    var found = [];

    // 5
    found.push($(":input[name^=foo]").size());

    // uncaught exception: Syntax error, unrecognized expression: ]
    //found.push($(":input[name^=foo[]").size());

    // undefined
    try{
        found.push($(":input[name^=foo[]").size());
    }catch(e){
        found.push("Exception: " + e.description);
    }

    // uncaught exception: Syntax error, unrecognized expression: ]
    //found.push($(":input[name^=foo\[]").size());

    // undefined
    try{
        found.push($(":input[name^=foo\[]").size());
    }catch(e){
        found.push("Exception: " + e.description);
    }

    // 0
    found.push($(":input[name^=foo\\[]").size());

    alert("Items found:\n" + found.join("\n"));
});
//--></script>
</head>
<body>

<p><input type="text" name="foo[-1]" value="A"></p>
<p><input type="text" name="foo[0]" value="B"></p>
<p><input type="text" name="foo[1]" value="D"></p>
<p><input type="text" name="foo[]" value="D"></p>
<p><input type="text" name="foobar" value="Z"></p>


</body>
</html>


Try to quote the value:

":input[name^='foo[']"
0

精彩评论

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

关注公众号