簡體   English   中英

如何使用jQuery刪除textarea中的值

[英]how to remove the value in the textarea using jquery

嗨,我在div表中有一行,如下所示:

<div class="tbody plb" id="impemail">
    <div class="tbc1" style="border-right:none;">
        <input class="tblinput sname pvar" type="text">
        <input type="hidden" class="ppre" value="">
    </div>
    <div class="thc2" style=" width: 75%; border-left:1px dotted #CFCFCF;">
        <textarea class="tblinput semails txtInputta pvar" style="font-size:13px;"></textarea>
        <input type="hidden" class="ppre" value="">
        <div class="errmsg emailerr"></div>
    </div>
    <div class="hideRow" style="width:20px;float:right;padding:15px 0px 0px 0px;">
        <img src="../../../images/redcross.png" alt="" />
    </div>
</div>

並且我嘗試編寫函數以使用jQuery函數單擊類“ hideRow”時刪除此行,如下所示,這里我想在hideRow函數進行時清除輸入和textarea字段,以便刷新頁面后值不應在該行中存在。 我嘗試的jQuery函數如下:

 $(function () {
     // Delete row from PTC grid 
     $('.hideRow').live("click", function () {
         $(this).parents('.plb').hide("slow", function () {
             $(this).parents('.tblinput sname pvar').val('');
             $(this).parents('.tblinput semails txtInputta pvar').val('');
         });
     })
 });

任何人都請告訴我如何清除這兩個字段,以便在頁面重新加載后不存在這些值。

如下更改您的選擇器:

$(this).parents('.tblinput.sname.pvar').val('');
$(this).parents('.tblinput.semails.txtInputta.pvar').val('');

對於一個元素的多個class ,您需要使用dot(.)將這些class名連接起來,而沒有任何空間像上面那樣在它們之間進行選擇。


您的選擇器在做什么

選擇器.tblinput sname pvardescendant selector格式。 這意味着它在tblinput snamesname搜索pvar ,第二個相同。


相關參考:


根據評論

$(function () {
     // Delete row from PTC grid 
     $('.hideRow').live("click", function () {
         $(this).closest('.plb').hide("slow", function () {
           $(this).find('.tblinput.sname.pvar, .tblinput.semails.txtInputta.pvar').val('');
         });
     })
 });

您的主要問題是,在.hide()的回調中, this的值表示隱藏的元素。 正確遍歷技術.find()$(this).find('.tblinput.sname.pvar').val('');

Java腳本

 $(function () {
     // Delete row from PTC grid

     // .live() is deprecated, port to .on()
     $('.hideRow').live("click", function () {

         //use .closest() instead of .parents() - less traversing
         $(this).closest('.plb').hide("slow", function () {

             // search down, not up - and no space between class selectors
             $(this).find('.tblinput.sname.pvar').val('');
             $(this).find('.tblinput.semails.txtInputta.pvar').val('');
         });
     })
 });

暫無
暫無

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

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