[英]How to get an element by classname out of a form when input is focused
<form id="formulier" class="cart" method="post" action="<?php echo makeLink(getCurrentItem()); ?>">
<input type="hidden" value="productid" name="productId">
<tr class="lol">
<td><img src="" width="125" alt="" /></td>
<td><h3 class="no-mt"><?php echo $cart['name'] ?></h3>
<p>
<?php echo substr($cart['description'],0 ,100) ?>
</p></td>
<td class="add-to-basket">
<input type="number" id="modify-number" name="quantity" value="<?php echo $cart['quantity'] ?>" min="1" required />
<input type="hidden" id="productId" name="productId" value="<?php echo $cart['productId'] ?>" />
</td>
<td class="add-to-basket-button">
<fieldset>
<button type="submit" id="remove" name="remove">
<i class="fa fa-trash-o fa-lg"></i>
</button>
</fieldset></td>
<?php $price = $cart['quantity'] * $cart['price'] ?>
<td class="price"><?php echo number_format($price, 2)?></td>
</tr>
</form>
因此,當#modify-number
為focused
,我要禁用#remove button
。
我嘗試這樣做:
$('#modify-number').focus(function() {
var price = $(this).parents('#formulier').find('#remove');
price.prop('disabled', true);
})
但這似乎不起作用。
有人可以幫助我嗎?
$('#modify-number').focus(function() {
// your other stuff
$('#remove').attr('disabled',true);
})
根據OP的評論進行 更新
$('#modify-number').focus(function() {
// your other stuff
$(this).closest('form').find('button[type="submit"]').attr('disabled',true);
})
您需要定位id
為remove
而不是class price
為td
的button
:
$('#modify-number').focus(function() {
$('#remove').prop('disabled', true);
})
根據您的評論,似乎您有重復的id
, id
必須是唯一的,您需要使用class更改所有重復的id
:
<button type="submit" class="remove" name="remove">
<i class="fa fa-trash-o fa-lg"></i>
</button>
以上只是更改button
元素的重復ID,您還需要更改其他元素,然后可以使用:
$('#modify-number').focus(function() {
var removeBtn = $(this).closest('.formulier').find('.remove');
removeBtn.prop('disabled', true);
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.