简体   繁体   English

删除 jquery 中的文本字段值

[英]Remove text field value in jquery

when i click on add it show the text field with value.当我点击添加时,它会显示带有值的文本字段。 And when i click on remove its hide.当我点击删除它的隐藏。 But i want to remove text field value also when i click on remove.但是当我点击删除时,我也想删除文本字段值。

Css css

#second {
    display: none;
}
#third {
    display: none;
}
#forth {
    display: none;
}
#fifth {
    display: none;
}

html html

<div id="header">
     <a href="#" id="add1">add</a> - <a href="#" id="remove">remove</a>
    <div id="first" class="toggle"><input type="text" value="1" name="sid[]">first</div>            
    <div id="second" class="toggle"><input type="text" value="2" name="sid[]">second</div>
    <div id="third" class="toggle"><input type="text" value="3" name="sid[]">third</div>
    <div id="forth" class="toggle"><input type="text" value="4" name="sid[]">forth</div>
    <div id="fifth" class="toggle"><input type="text" value="5" name="sid[]">fifth</div>
</div>

Jquery查询

$(document).ready(function() {
        $("#add1").click(function() {
            $('.toggle:not(:visible)').first().show();
        });
        $("#remove").click(function() {
            $('.toggle:visible').last().hide();

        });
    });

Here is my code : Jsfiddle这是我的代码Jsfiddle

just add .find('input').val('');只需添加.find('input').val(''); after .hide();.hide(); to be成为

$('.toggle:visible').last().hide().find('input').val('');

DEMO演示

try this code:-试试这个代码:-

<script>
$(document).ready(function() {
        $("#add1").click(function() {
            $('.toggle:not(:visible)').first().show();
        });
        $("#remove").click(function() {
         $('.toggle:visible').last().find(':input').val('');
            $('.toggle:visible').last().hide();


        });
    });
</script>

hi now used to .find() and .val() as like this嗨,现在习惯.find().val()像这样

$(document).ready(function() {
        $("#add1").click(function() {
            $('.toggle:not(:visible)').first().show();
        });
        $("#remove").click(function() {
            var valNon = $('.toggle:visible').last().hide();
               valNon.find('input').val(''); // add this line   

        });
    });

use this用这个

$(document).ready(function() {
        $("#add1").click(function() {
            $('.toggle:not(:visible)').first().show();
        });
        $("#remove").click(function() {
           $('.toggle:visible').last().hide().find('input').val('');
        });
    });

Jsfiddel杰斯菲德尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM