【问题标题】:how to remove the value in the textarea using jquery如何使用jquery删除textarea中的值
【发布时间】:2012-09-13 07:22:58
【问题描述】:

嗨,我的 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 函数运行时清除输入和文本区域字段,以便在刷新后页面中的值不应该在行中。 我试过的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('');
         });
     })
 });

任何人请告诉我如何清除这两个字段,以便在页面重新加载后这些值不应该存在。

【问题讨论】:

    标签: jquery html


    【解决方案1】:

    更改您的选择器,如下所示:

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

    对于一个元素的多个classes,您需要使用dot(.) 将这些class 名称连接起来,并且没有任何空格,以便在它们之间创建一个像上面一样的选择器。


    你的选择器在做什么

    您的选择器.tblinput sname pvardescendant selector 格式。这意味着它在sname 中搜索pvartblinput 中的sname,第二个搜索相同。


    相关参考文献:


    根据评论

    $(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('');
             });
         })
     });
    

    【讨论】:

    • 不,这对我没有帮助....之前我试过像 $('.tblinput.sname.pvar').val('');而不是 $(this).parents('.tblinput.sname.pvar').val('');然后我能够清除该类的所有字段,但我只想清除这一行如何做到这一点,使用 $(this).parents('').val(); 是否有错误?像这样??
    • 我特别感谢@thecodeparadox.....我参考了这些选择器并得到了解决方案......:)
    【解决方案2】:

    您的主要问题是,在.hide() 的回调中,this 的值是指被隐藏的元素;正确的遍历技术是.find(),如$(this).find('.tblinput.sname.pvar').val('');

    Javascript

     $(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('');
             });
         })
     });
    

    【讨论】:

    • 使用.on()而不是.live()的语法是$(document).on('click', '.hideRow', handler),你可以用.hideRow的最接近的静态父元素替换document
    猜你喜欢
    • 2021-01-04
    • 2016-12-06
    • 2020-05-17
    • 2011-05-17
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    相关资源
    最近更新 更多