我有一个问题,
<a id="shoppingLink1" href="?page=bestellen&action=add&id=2">
<img src="images/shoppingcartButton.png" class="shoppingButton" alt="shoppingcartButton" title="shoppingcartButton"/>
</a><br/>
在jquery中,当我单击shoppingButton时,我想从id“shoppingLink”中选择最后一个字符。 “1”。
这些是动态添加的,所以如果达到 10 个,它应该自动选择最后 2 个字符。谁能帮帮我?
最佳答案
$('.shoppingButton').click(function () {
var id = parseInt($(this).parent('a[id^=shoppingLink]').attr('id').match(/\d+$/)[0], 10);
// Do stuff.
return false;
});
这使用 RegExp 来匹配 $(this).parent('a[id^=shoppingLink]').attr('id')
末尾的所有数字(即 >.shoppingButton
的父 a
的 id
属性,其中 id
以 "shoppingLink"
开头>)。然后,它使用 parseInt
将这些数字解析为数字。
关于jquery - 在jquery中选择一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3433843/