通过正则表达式 jQuery 按 id 获取元素
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,由于我们都建议相同代码的不同版本,所以这是我的:
我还对迄今为止发布的所有建议进行了一些速度测试。我的恰好是 Firefox 和 Chrome 上最快的,而 IE8 上第二好的。乔什·斯托多拉的根本不起作用。
这是测试套件: http://jsbin.com/igate (可通过 http://jsbin.com/igate/edit)
以下是结果:
Firefox
Chrome
IE8
Well, since we are all suggesting different versions of the same code, here's mine:
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
Chrome
IE8
怎么样,
我认为您对太多引号感到困惑!
How about
I think you've got confused with too many quotation marks!
我将使用startsWith选择器来获取元素的名称空间,然后查询it来获取匹配项...
I would use the startsWith selector to get the namespace of elements, and then query against it to get your matches...
您可能会搜索两次
You might search twice