开发者

How can alter the value of every single input element, by a fixed amount, without using a loop?

开发者 https://www.devze.com 2023-01-21 17:00 出处:网络
I want select every inputbox o开发者_运维技巧f type text and transform by a fixed amount, when checkbox is checked, so far I have this. However I wondered if there was a way to do this without iterati

I want select every inputbox o开发者_运维技巧f type text and transform by a fixed amount, when checkbox is checked, so far I have this. However I wondered if there was a way to do this without iterating over every element ...

    $("#myID").change(function(){

       if($(this).is(':checked')){

           //I can't do this :
           $("input:text").val( $("input:text").val()*4);     

          //I have to do this ?
          $("input:text").each(function(index){
               $(this).val($(this).val()*4);
           });

    });


If you are using jQuery 1.4+, you can use a function callback to calculate the value:

$("input:text").val(function(idx, oldVal) {
    return oldVal * 4;
});

See the API.

0

精彩评论

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