admin管理员组

文章数量:1399887

I want to clear the input value with class "inputF" generated by the append in jQuery,

<script type="text/javascript" src="jquery.min.js"></script>
<script>
$(document).ready(function () {
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID

    $(add_button).click(function (e) { //on add input button click
        e.preventDefault();

        $(wrapper).append('<div><input type="text" class="inputF" name="mytext[]"/>\n\
                <button name="remove" class="remove_field">x</button>\n\
                <button name="clear" class="clear_field">clear</button>\n\
                <input type="checkbox" class="check" group="check">\n\
              </div>'),
    });

});
</script>
<div class="input_fields_wrap">
<button class="add_field_button">+</button>
<button class="show_field_button">show all</button>

This didn't worked for me,

    $(wrapper).on("click", ".clear_field", function () {
        $(this).find('.inputF').val('');
    });

What is the proper way to write it?

I want to clear the input value with class "inputF" generated by the append in jQuery,

<script type="text/javascript" src="jquery.min.js"></script>
<script>
$(document).ready(function () {
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID

    $(add_button).click(function (e) { //on add input button click
        e.preventDefault();

        $(wrapper).append('<div><input type="text" class="inputF" name="mytext[]"/>\n\
                <button name="remove" class="remove_field">x</button>\n\
                <button name="clear" class="clear_field">clear</button>\n\
                <input type="checkbox" class="check" group="check">\n\
              </div>'),
    });

});
</script>
<div class="input_fields_wrap">
<button class="add_field_button">+</button>
<button class="show_field_button">show all</button>

This didn't worked for me,

    $(wrapper).on("click", ".clear_field", function () {
        $(this).find('.inputF').val('');
    });

What is the proper way to write it?

Share Improve this question edited May 19, 2016 at 14:30 Keyur asked May 19, 2016 at 14:28 KeyurKeyur 1,1213 gold badges23 silver badges42 bronze badges 1
  • Vesion of jQuery is v1.12.3 – Keyur Commented May 19, 2016 at 14:33
Add a ment  | 

3 Answers 3

Reset to default 2

While your html structure looks like

<div>
     <input type="text" class="inputF" name="mytext[]"/>
     <button name="remove" class="remove_field">x</button>
     <button name="clear" class="clear_field">clear</button>
     <input type="checkbox" class="check" group="check">
</div>

you need to use .parent();

$(wrapper).on("click", ".clear_field", function () {
     $(this).parent('div').find('.inputF').val('');
});

or .closest();

$(wrapper).on("click", ".clear_field", function () {
     $(this).closest('div').find('.inputF').val('');
});

You have incorrect selector to target sibling element:

$(wrapper).delegate("click", ".clear_field", function () {
    $(this).closest('div').find('.inputF').val('');
});

Just replace :

$(this).find('.inputF').val('');

With :

$(wrapper).find('.inputF').val('');

本文标签: javascriptClear input value on click jqueryStack Overflow