簡體   English   中英

輸入集中時如何從類中從類中獲取元素

[英]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-numberfocused ,我要禁用#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);
})

您需要定位idremove而不是class pricetdbutton

$('#modify-number').focus(function() {
    $('#remove').prop('disabled', true);
})

根據您的評論,似乎您有重復的idid必須是唯一的,您需要使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM