简体   繁体   English

jQuery隐藏最接近的按钮

[英]jquery hide closest button

I am learning jQuery and I'm stuck on this. 我正在学习jQuery,但仍坚持下去。

I am trying to hide button when input value is 200, but there are other inputs too. 我试图在输入值为200时隐藏button ,但是也有其他输入。 This question has been asked earlier but I couldn't figure it out in my case. 这个问题已经被提过了,但是我无法解决。

 $("document").ready(function() { setTimeout(function() { var test = $('.input-text').val(); if (test = 200) { $('button').hide(); } }, 10); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="first"> <input class="input-text qty text" type="number" readonly="" value="200"> <button> Hide me </button> </div> <div class="second"> <input class="input-text qty text" type="number" readonly="" value="300"> <button> Hide me </button> </div> <div class="third"> <input class="input-text qty text" type="number" readonly="" value="400"> <button> Hide me </button> </div> 

Try like following. 像下面一样尝试。

 $("document").ready(function() { setTimeout(function() { $('.input-text').each(function() { if (this.value == 200) { $(this).next('button').hide(); } }); }, 10); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="first"> <input class="input-text qty text" type="number" readonly="" value="200"> <button> Hide me </button> </div> <div class="second"> <input class="input-text qty text" type="number" readonly="" value="300"> <button> Hide me </button> </div> <div class="third"> <input class="input-text qty text" type="number" readonly="" value="400"> <button> Hide me </button> </div> 

You have a few issues in your logic. 您的逻辑中有一些问题。

  • setTimeout is unnecessary setTimeout是不必要的
  • val() returns a string which you're comparing to an int, which will give some unreliable outcomes. val()返回要与int比较的字符串,这将导致某些不可靠的结果。
  • there are multiple input elements, so you need to evaluate their values individually. 有多个输入元素,因此您需要分别评估它们的值。
  • you're hiding all button elements, you need to traverse the DOM to find the button related to each input 您要隐藏所有button元素,则需要遍历DOM来查找与每个input相关的button

With that said, this should work for you: 话虽如此,这应该为您工作:

 $("document").ready(function() { $('.input-text').each(function() { if (parseInt($(this).val(), 10) == 200) { $(this).next('button').hide(); }; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="first"> <input class="input-text qty text" type="number" readonly="" value="200"> <button> Hide me </button> </div> <div class="second"> <input class="input-text qty text" type="number" readonly="" value="300"> <button> Hide me </button> </div> <div class="third"> <input class="input-text qty text" type="number" readonly="" value="400"> <button> Hide me </button> </div> 

You have a number of issues. 您有很多问题。

var test = $('.input-text').val(); Will only ever retrieve the value of the first .input-text 只会检索第一个.input-textvalue

if (test = 200) is assigning test = 200 rather than evaluating ( == ). if (test = 200)正在分配test = 200而不是评估( == )。

$('button').hide(); will hide every button on the page. 将隐藏页面上的每个按钮。

You could filter the buttons, and return the ones whose previous input value is 200, and then hide them: 您可以过滤按钮,并返回先前输入值为200的按钮,然后将其隐藏:

$('.input-text + button').filter( function() {
  return $(this).prev().val() == 200;
}).hide();

Here's a fiddle 这是一个小提琴

使用jQuery.nearest

$('#id').closest(".classname");
$("document").ready(function() {
    setTimeout(function() {
       $('.input-text').each(function(){
       var test = $(this).val();
       if(test == 200) {
          $('.input-text').next("button").hide();
       }
});

    },10);
});

You need to check sibling of all .input-text where condition is met with next() . 您需要检查所有.input-text是否满足next()

next() selects the first sibling of element you selected depending on condition ( if .val == 200 ). next()根据条件( if .val == 200 )选择所选元素的第一个同级。 And then it makes that sibling gone. 然后,它使兄弟姐妹消失了。

Also, you should test all .input-text elements with each() . 同样,您应该使用each()测试所有.input-text元素。

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

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