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
3 Answers
Reset to default 2While 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
版权声明:本文标题:javascript - Clear input value on click jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744216191a2595649.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论