我编写了一个代码,当用户输入他想要的产品数量时,我试图在其中添加产品数量。问题是我无法通过 jquery 选择 anchor
来获取 href
链接,以便我可以为其添加新值。
我的代码:
$('.mival').keyup(function(event) {
var i=$(this).parents(".cart").next('.milink').attr('href');
alert(i);
});
当我尝试提醒它说 undefined
我的 html 标记:
<div class="cart">
<div><strong>Price:</strong> <span>$20.30</span></div>
<div><strong> Qty:</strong><span>
<input type="text" class="mival" value="1" />
</span></div>
<a class="milink" href="http://example.com/vid=3">Add to Cart </a> </div>
我还没有编写将值添加到链接的代码我可以做到。问题是如何从此标记的 a
元素中获取值,以便我可以获得 href
链接。
最佳答案
您不
有任何父类cart
,使用div 代替可以做到这一点。或者,您可以使用 closest('.container')
$('.mival').keyup(function(event) {
var i=$(this).parents("div").next('.milink').attr('href');
alert(i);
});
关于jquery - 获取 href 链接并通过 jQuery 为其添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15495598/