开发者

Quick JQuery Question - change value with .find

I have this code.

$(".setHide").click(function () {
  $('.newSet').find('input[type=text]').val('');
  $(this).parent().hide();

It will detect a click on a link with the .setHide class and then:

1) Empty any field inside the .newSet div

2) Hide the .newSet div

My issue is that I have several .newSet开发者_如何学Python divs open, so all the fields in all the divs get emptied. How can I empty only the fields in the current div in which the .setHide link is clicked?

Thanks for the help!


Instead of this:

$('.newSet').find('input[type=text]').val('');

try this:

$(this).find('input[type=text]').val('');


Use the parents() method, see how it works at the jQuery Documentation

$(".setHide").click(function () {
  $(this).parents(".newSet")
         .hide()
         .find('input[type=text]')
         .val('');
})


$(".setHide").click(function(){
  var $div = $(this).parent().hide();
  $('.newSet input[type="text"]', $div).val('');
});

This will clear all inputs inside .newSet that are inside the parent of .setHide.


You can give the selector an element to start searching from and in this instance you want it to start from the current element.

$('.newSet', this).find('input[type=text]').val('');


$(this).closest('.newSet').find('input[type=text]').val('');


$('.setHide').click(function() {
  var $newSet = $('.newSet', $(this));
  $newSet.hide().find('input[type=text]').val('');
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜