0

私はjqueryが初めてです。入力テキストの値を変更していて、次の入力テキストの値を変更する必要があるため、コードは次のとおりです。

$(document).ready(function() {   

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

        $(this).next('input').val("newvalue");
    });

});

しかし、何も... !


編集: 以下のコメントから HTML を投稿する

<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>
4

4 に答える 4

4

編集:あなたが提供した更新された情報に基づいて、これはうまくいくはずです:

$('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 );
});

元のソリューション:

マークアップを見ないとわかりにくいですが、この 2 つの間に何らかの要素があると思います。

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

HTML を見ないと、これ以上の答えは得られません。

セレクターを から に変更したことに注意してください'[type=text]''input[type=text]'これにより、より効率的になります。

于 2010-12-22T18:09:37.220 に答える
0

これにより、それらすべてが次のように変更されます。

$("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/

于 2010-12-22T18:09:16.347 に答える
0

試してみてください:

$(this).attr('value',"newvalue");
于 2010-12-22T18:10:44.730 に答える
0

理由はわかりませんが、あなたのソリューションは良いように見えましたが、next()機能に成功しませんでした。

最後に私はこれをしました:

$('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);}
         });

});
于 2010-12-22T22:27:12.583 に答える