简体   繁体   English

使用 data 属性获取值并显示在其他 div 上

[英]get value with data attribute and display on other div

I would like to show the value "1,000 below" found inside the button on this div using jquery我想使用 jquery 显示在此 div 上的按钮内找到的值“低于 1,000”

<div class="showvaluehere">here</div>

<button class="button" data-filter=".1000">1,000 below</button>

I got this so far but not working, it shows the class ".1000"到目前为止我得到了这个但没有工作,它显示类“.1000”

var syo = $this.attr('data-filter');
$(this).parent().find('.showvaluehere').html(syo);

for clarity, I want the value of the button to be on the为清楚起见,我希望按钮的值在

so that it shows like this;以便它显示为这样;

<div class="showvaluehere">1,000 below</div>

Thanks谢谢

You are getting value of data-filter attribute, which is .1000您正在获取数据过滤器属性的值,即 .1000

Try this尝试这个

   $('.button').click(function() {
      var syo = $(this).text();
      $('.showvaluehere').html(syo);
    })

Your code is confusing as you're referencing a lot of elements and attributes which seem unrelated to the HTML you've shown and the problem you describe.您的代码令人困惑,因为您引用了许多似乎与您显示的 HTML 和您描述的问题无关的元素和属性。

That said, you can do what you require by simply setting the text() of the .showvaluehere element to match that of it's neighbouring button , like this:也就是说,您可以通过简单地将.showvaluehere元素的text()设置为与其相邻的button相匹配来执行所需的操作,如下所示:

 $('.showvaluehere').text(function() { return $(this).next('button').text(); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="showvaluehere">here</div> <button class="button" data-filter=".1000">1,000 below</button>

 $('.button').click(function() { $('.showvaluehere').text($(this).text()) })
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="showvaluehere">here</div> <button class="button" data-filter=".1000">1,000 below</button>

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

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