我在Magento的项目正在与一个插件变化对(document).ready()
的onclick
属性。我尝试使用jquery在用户操作后更新它,但它返回undefined
。而且我注意到,onclick属性不是由view page source
或Ctrl + u
访问的页面的源代码中的检查可见,但可见。onclick属性不能在jQuery中进行选择,并在检查中不可见,但工作
在检查的有针对性的按钮看起来像
<button type="button" id="addtocart_btn_5" title="Add to cart" class="button btn-cart btn-cart-with-qty">
<span>Add to cart<span>
</button>
在页面源
查看它看起来像:
<button type="button" id="addtocart_btn_5" title="Add to cart" class="button btn-cart btn-cart-with-qty"
onclick="setLocation('http://mysite.local/eg_ar/checkout/cart/add/uenc/aHR0cDovL2hhaXJidXJzdGFyYWJpYS5sb2NhbC9lZ19hci8,/product/5/form_key/lqUtnptjQU7Cn7E1/qty/1/')">
<span>Add to cart</span>
</button>
这个按钮是通过可变btn
正确访问,当我使用console.log(btn.attr("onclick"));
返回undefined
但像id
属性它正确返回ID。
注意:该按钮工作得非常好。但我无法编辑它。
更新: 按@Saptal的问题,我已经表现出更多的代码。在HTML部分我有这个块
<div class="add-to-cart">
<form id="product_addtocart_form_15">
<span class="custom-counter">
<input min="1" name="qty" id="qty" maxlength="2" value="1" title="qty" class="qty qty-updating" type="number"><div class="custom-counter-nav"><div class="custom-counter-button custom-counter-up">+</div><div class="custom-counter-button custom-counter-down">-</div></div>
</span>
<button type="button" id="addtocart_btn_15" title="Add to cart" class="button btn-cart btn-cart-with-qty"><span>Add to cart</span></button>
</form>
</div>
和JS我做
<script>
jQuery('.qty-updating').each(function(){
var qty = jQuery(this);
qty.on('change', function(e){
alert('here');
var btn = qty.parent().siblings('.btn-cart');
console.log(btn.attr("onclick"));
});
});
</script>
在此先感谢
你是如何得到'btn' – Satpal
@Satpal我更新了这个问题,并且添加了更多代码 –
用'let qty'尝试一次' – Satpal