通过正则表达式 jQuery 按 id 获取元素

发布于 2024-08-07 07:26:36 字数 978 浏览 2 评论 0原文

Html 元素包含复杂的 &唯一的 id,由作为前缀的命名空间和作为后缀的递增索引组成。

我尝试使用 id 的通配符表达式来实现。

如果使用没有命名空间的简单方法,它工作得很好:

$j("[id*=service_selected_]").click(function(){
...

为了保持唯一性,我需要在 id 中提供命名空间部分,但它不会以这种方式解析:

var selId = "'" + '[id*=' + namespace + '_service_selected_]' + "'";

$j(selId).click(function(){
...

在 .jsp 部分下面:

        <c:forEach var="package" items="${packages.page}" varStatus="status">
            <tr>
                <td>
                    ${package.name}
                </td>
                <td id="<portlet:namespace/>_service_price_${status.index}">${package.price}</td>
                <td >
<input type="checkbox" name="service_id" value="${package.id}" id="<portlet:namespace/>_service_selected_${status.index}">
                </td>
            </tr>
        </c:forEach>

Html element contains complex & unique id, composed from namespace as prefix and incremented index - as postfix.

I try do it with wildcard expression for id.

If to use simple way without namespace, that it works fine:

$j("[id*=service_selected_]").click(function(){
...

In order to keep uniqueness,i need provide namespace part within id,but it doesn't resolved this way:

var selId = "'" + '[id*=' + namespace + '_service_selected_]' + "'";

$j(selId).click(function(){
...

Below the .jsp part:

        <c:forEach var="package" items="${packages.page}" varStatus="status">
            <tr>
                <td>
                    ${package.name}
                </td>
                <td id="<portlet:namespace/>_service_price_${status.index}">${package.price}</td>
                <td >
<input type="checkbox" name="service_id" value="${package.id}" id="<portlet:namespace/>_service_selected_${status.index}">
                </td>
            </tr>
        </c:forEach>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

年华零落成诗 2024-08-14 07:26:36

好吧,由于我们都建议相同代码的不同版本,所以这是我的:

var namespace = 'test';
var id = '_service_selected_';
$(["[id^=", namespace, "_", id, "_]"].join(''));

我还对迄今为止发布的所有建议进行了一些速度测试。我的恰好是 Firefox 和 Chrome 上最快的,而 IE8 上第二好的。乔什·斯托多拉的根本不起作用。

这是测试套件: http://jsbin.com/igate (可通过 http://jsbin.com/igate/edit)

以下是结果:

Firefox

select_Phil 37 ms 36 selected
select_Ghommey 120 ms 36 selected
select_Josh_Stodola 1077 ms 0 selected
select_brianpeiris 31 ms 36 selected

Chrome

select_Phil 17 ms 36 selected
select_Ghommey 108 ms 36 selected
select_Josh_Stodola 290 ms 0 selected
select_brianpeiris 15 ms 36 selected

IE8

select_Phil 69 ms 36 selected
select_Ghommey 300 ms 36 selected
select_Josh_Stodola 1632 ms 0 selected
select_brianpeiris 73 ms 36 selected

Well, since we are all suggesting different versions of the same code, here's mine:

var namespace = 'test';
var id = '_service_selected_';
$(["[id^=", namespace, "_", id, "_]"].join(''));

I've also done some speed tests on all the suggestions posted so far. Mine happens to be the fastest on Firefox and Chrome and the second best on IE8. Josh Stodola's doesn't work at all.

Here's the test suite: http://jsbin.com/igate (Editable via http://jsbin.com/igate/edit)

Here are the results:

Firefox

select_Phil 37 ms 36 selected
select_Ghommey 120 ms 36 selected
select_Josh_Stodola 1077 ms 0 selected
select_brianpeiris 31 ms 36 selected

Chrome

select_Phil 17 ms 36 selected
select_Ghommey 108 ms 36 selected
select_Josh_Stodola 290 ms 0 selected
select_brianpeiris 15 ms 36 selected

IE8

select_Phil 69 ms 36 selected
select_Ghommey 300 ms 36 selected
select_Josh_Stodola 1632 ms 0 selected
select_brianpeiris 73 ms 36 selected
权谋诡计 2024-08-14 07:26:36

怎么样,

var selId = "[id*=" + namespace + "_service_selected_]";
$j(selId).click(function(){
...

我认为您对太多引号感到困惑!

How about

var selId = "[id*=" + namespace + "_service_selected_]";
$j(selId).click(function(){
...

I think you've got confused with too many quotation marks!

ヅ她的身影、若隐若现 2024-08-14 07:26:36

我将使用startsWith选择器来获取元素的名称空间,然后查询it来获取匹配项...

var namespace = "test";
var itemspace = "_service_selected_";

var ns = $("[id^=" + namespace + "]");
var nsItems = $("[id*=" + itemspace + "]", ns);

I would use the startsWith selector to get the namespace of elements, and then query against it to get your matches...

var namespace = "test";
var itemspace = "_service_selected_";

var ns = $("[id^=" + namespace + "]");
var nsItems = $("[id*=" + itemspace + "]", ns);
我们的影子 2024-08-14 07:26:36

您可能会搜索两次

$j("[id*=" + namespace + "]").filter("[id*=service_selected_]").click(function(){

You might search twice

$j("[id*=" + namespace + "]").filter("[id*=service_selected_]").click(function(){
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文