简体   繁体   English

jQuery的。 按类别名称检查每个项目

[英]jQuery. Check each item by class name

For example, there are some tags 例如,有一些标签

<input value="1000" class="chrome-input" />
<input value="2000" class="chrome-input" />

How can I make manipulation with each value? 如何使用每个值进行操作? Here I can set value to each input. 在这里,我可以为每个输入设置值。 $(".chrome-input").val("10"); But what if I want multiply each value by 10 . 但是,如果我想将每个值乘以10

Something like that $(".chrome-input").val($(".chrome-input").val()*10); 像这样的$(".chrome-input").val($(".chrome-input").val()*10);

And I want to get that 我想得到

<input value="10000" class="chrome-input" />
<input value="20000" class="chrome-input" />

The clean way of doing this is with the callback signature of val : 这样做的干净方法是使用val的回调签名:

$('.chrome-input').val(function(i, oldVal) {
    return parseInt(oldVal, 10) * 10;
});

If you pass a function as the first argument of val , that function will be called on each element. 如果将函数作为val的第一个参数传递,则将在每个元素上调用该函数。 The second argument passed will be the current value of the element. 传递的第二个参数将是元素的当前值。 The return value of the function will be set as the new value. 函数的返回值将被设置为新值。

$(".chrome-input").each(function() {
  var el = $(this);
  el.val(el.val() * 10);
});

You can use .each() like so: 您可以这样使用.each()

$('.chrome-input').each(function(i,ele){
  var $this = $(this);
  $this.val( parseInt($this.val(),10) * 10 );
});

Try this : http://jsfiddle.net/vdTT4/ 试试这个: http : //jsfiddle.net/vdTT4/

$(".chrome-input").each(function(){
    $(this).val($(this).val() * 10);
}); 
$(".chrome-input").each(function(){var value = $(this).val(); $(this).val(value*10); });

you need to use function each() 您需要使用功能each()

$('li').each(function(index) {
  alert(index + ': ' + $(this).text());
});

link to jQuery documentation 链接到jQuery文档

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

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