簡體   English   中英

清除輸入值Javascript

[英]Clear input value Javascript

我有兩個單選按鈕,foreach單選按鈕有文本輸入字段。 我將它添加為-------就像文本輸入字段的默認值一樣。 我只想在選中第二個收音機時刪除此值。

我的代碼是:

if (jQuery('.product-options .display ul li:nth-child(1)').find('input').is(':checked')) {
        jQuery(".product-options dd input.input-text").attr('value', '--------');
        jQuery(".product-options dd input.input-text").addClass('validation-passed');
}

jQuery('.product-options .display ul li:nth-child(1)').find('input').change(function(){
    if (jQuery(this).is(':checked')) {
            jQuery(".product-options dd input.input-text").attr('value', '--------');
            jQuery(".product-options dd input.input-text").addClass('validation-passed');

    }

})

jQuery('.product-options .display ul li:nth-child(2)').find('input').change(function(){
    if (jQuery(this).is(':checked')) {
            jQuery(".product-options dd input.input-text").attr('value', '');
    }
})

使用.val('')設置輸入的值,而不是.attr('value', '')

首先,您的代碼是一團糟。 您至少應該縮短選擇器,很難閱讀它們。 其次,我有一些問題要了解您的問題。

根據您的代碼的一些注釋。

1)在兩種情況下,您都將更改相同的輸入。 無論選中哪個按鈕,您仍然可以通過jQuery(".product-options dd input.input-text")搜索相同的元素

2) .val('')說了什么-您應該使用.val('')清除輸入值。 由於我的第一點,您的值是空白。 您只是在做錯選擇。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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