Javascript jQuery。按类名检查每个项
例如,有一些标签Javascript jQuery。按类名检查每个项,javascript,jquery,Javascript,Jquery,例如,有一些标签 <input value="1000" class="chrome-input" /> <input value="2000" class="chrome-input" /> 您可以像这样使用.each(): $('.chrome-input').each(function(i,ele){ var $this = $(this); $this.val( parseInt($this.val(),10) * 10 ); }); 您需要使用函数e
<input value="1000" class="chrome-input" />
<input value="2000" class="chrome-input" />
您可以像这样使用.each()
:
$('.chrome-input').each(function(i,ele){
var $this = $(this);
$this.val( parseInt($this.val(),10) * 10 );
});
您需要使用函数
each()
试试这个:
执行此操作的干净方法是使用回调签名:
如果将函数作为
val
的第一个参数传递,则每个元素都将调用该函数。传递的第二个参数将是元素的当前值。函数的返回值将设置为新值。…如果用户键入“0x100”?(或者,在某些实现中,更合理的说法是“01000”)取决于您为谁编写。我很好,01000*10等于5120.:)这就是为什么我将它缓存到一个变量中,但这一点很好。当我发表评论时,您最初并没有这样做。可能最好使用parseInt
,来抵御像“0x100”和(在某些实现中)“010”这样的奇怪输入。哦,这很酷。val()接受回调。谢谢你@Amadan值得一提的是,大多数设置值/属性的jQuery函数都是这样做的。
$(".chrome-input").each(function() {
var el = $(this);
el.val(el.val() * 10);
});
$('.chrome-input').each(function(i,ele){
var $this = $(this);
$this.val( parseInt($this.val(),10) * 10 );
});
$('li').each(function(index) {
alert(index + ': ' + $(this).text());
});
$(".chrome-input").each(function(){
$(this).val($(this).val() * 10);
});
$(".chrome-input").each(function(){var value = $(this).val(); $(this).val(value*10); });
$('.chrome-input').val(function(i, oldVal) {
return parseInt(oldVal, 10) * 10;
});