开发者

put a value to the next item with jquery

I'm new with jquery. I'm changing the value of an input text and I need to change the value of the next input text, so my code is :

$(document).ready(function() {   

$('[type=text]').change(function() {

   开发者_如何学JAVA     $(this).next('input').val("newvalue");
    });

});

But nothing... !


EDIT: Posting HTML from comment below

<form method="post" action="/volume/create"> 
    <table> 
        <tr> 
            <th><label for="volume_h1">H1</label></th> 
            <td><input type="text" id="volume_h1" name="volume[h1]"></td> 
        </tr> 
        <!-- .............. to volume_h24 -->
        <tr> 
            <th><label for="volume_h24">H24</label></th>
            <td><input type="text" id="volume_h24" name="volume[h24]"></td> 
        </tr> 
    </table> 
</form>


EDIT: Based on the updated information you provided, this should work:

$('input[type=text]').change(function() {
    var inputs = $(this).closest('form').find('input:text');
    var nextIndex = inputs.index( this ) + 1;
    inputs.slice( nextIndex ).val( this.value );
});

Original solution:

Hard to tell without seeing your markup, but I'm guessing you have some element in between the two.

$('input[type=text]').change(function() {
    $(this).nextAll('input:first').val("newvalue");
});

Can't give much more of an answer without seeing your HTML.

Note that I changed the selector from '[type=text]' to 'input[type=text]', which will be more efficient.


Try By :

$(this).attr('value',"newvalue");


This will change all of them down the line:

$("input:text").change(function() {
    var v = $(this).next("input:text");
    v.val($(this).val());
    v.trigger('change');
})

HTML:

<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">

http://jsfiddle.net/uDgnt/


I don't know why, your solutions seemed to be good but i didn't suceed with the next() function.

Finally i did this :

$('input[type=text]').change(function() {
       //get all the input text of the form
    var yourFormFields = $('#newform').find(':input[type=text]');
      // the index of my changed input
    $index = yourFormFields.index( this );
     //the value i entered
    $mavalue = $(this).val();

       $.each( yourFormFields, function(i, n){
    if(i>$index)
    {yourFormFields.eq(i).val($mavalue);}
         });

});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜