删除jquery中的文本字段值


Remove text field value in jquery

当我点击add时,它会显示带有值的文本字段。当我点击删除它的隐藏。但当我点击删除时,我也想删除文本字段值。

Css

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

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

这是我的代码:Jsfidle

只需在.hide();之后添加.find('input').val('');即可成为

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

DEMO

尝试以下代码:-

<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>

使用此

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

Jsfaddel

hi现在使用.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   
        });
    });