2016-12-28 5 views
-1

は、だから私は何をしたいのか、同じ入力値で、テキスト入力を埋めています。ここにはフォームの例があります。IDのないすべてのテキスト入力を同じ値で入力するにはどうすればよいですか?

<button class="btn">Fill All</button> 
    <input type ="text" class="inpt" ></input> 
    <input type ="text" class="inpt" ></input> 
    <input type ="text" class="inpt" ></input> 
    <input type ="text" class="inpt" ></input> 

私は入力の名前を取り、それをそのように充満何javascriptの例を見て、ここではIDによって、これを何jQueryの例を見ました:copy text box content to another textbox while typingが、割り当てられたIDが存在しないとき、私はこれをどのように行うことができますし、すべてのクラスと名前は同じ値を持ちます。私はJquery、jsの初心者であり、それを理解するにはあまりにも愚かであり、そのための例も見つけられませんでした。

編集:すてきな答えてくれてありがとう、しかし、最初のまたは任意のテキストボックスなどから値を取得し、その値を他の人を埋めるための方法があります。

+0

'' ---ない有効な –

+0

'$( " BTN")、セレクタ機能を使用しnextAll( 「.inpt 『)。valを(』何か」) ' –

答えて

0

これは何が必要でしょうか?

$(function(){ 
 
    $('.btn').on('click', function(){ 
 
    var $value = $('input').val(); 
 
    $(".inpt").val($value); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 
<button class="btn">Fill All</button> 
 
<input type ="text" class="inpt" /> 
 
<input type ="text" class="inpt" /> 
 
<input type ="text" class="inpt" /> 
 
<input type ="text" class="inpt" />

+0

種類のが、私は最初の入力フィールドなどから値を取得し、その値を次の入力を埋めることができ方法は何ですか? – Skyhigh17

+0

iは、すべての入力が正しい値ペースト]ボタンをクリックし、最初の入力値を入力したい、それを理解しませんか? –

+0

はかなりええ、それが可能かどう – Skyhigh17

0

は、入力フィールドにonkeyupイベントの使用を作成し、他の人に を値をコピーもう一つは、inputはシングルトンHTMLタグであり、したがって、あなたはむしろ</input><input {some parameters here}/>のように近くにありません。

$(function(){ 
 
    $('.inpt').on('keyup', function(){ 
 
    var val = $(this).val(); 
 
    $('.inpt').val(val); 
 
    }) 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type ="text" class="inpt" /> 
 
    <input type ="text" class="inpt" /> 
 
    <input type ="text" class="inpt" /> 
 
    <input type ="text" class="inpt" />

0

それを行うことができる方法だけ例:

keyupエイミー入力にバインドさ、それが価値だ保存し、それの価値を持つ他のすべての入力を交換し、その後再び注力ユーザーが入力を続けることができるように同じ入力。

$(function(){ 
 
    $('input[type="text"]').on('keyup', function(){ 
 
     value = $(this).val(); 
 
     $('input[type="text"]').val(value); 
 
     $(this).focus(); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button class="btn">Fill All</button> 
 
    <input type ="text" class="inpt" /> 
 
    <input type ="text" class="inpt" /> 
 
    <input type ="text" class="inpt" /> 
 
    <input type ="text" class="inpt" />

0

あなたはクラスを持っている場合は、あなたが試すことができます:

$('input').keyup(function() { 
    var txtClone = $(this).val(); 
    $('input[class$=inpt]').val(txtClone); 
}); 
0

はここであなたの質問、幸運を解決する簡単なコードです!

HTML:

<input type="button" class="btn" onclick="fillAll()" value="Fill All"></input> 
<input type ="text" class="inpt" /> 
<input type ="text" class="inpt" /> 
<input type ="text" class="inpt" /> 
<input type ="text" class="inpt" /> 

Javascriptを:。

function fillAll(){ 

     var inputArray = document.getElementsByTagName("input"); 

     for(var i = 0; i < inputArray.length; i++){ 
     if(inputArray[i].getAttribute("type") != "button"){ 
      inputArray[i].value = "hello!"; 
     } 
     } 

    } 
関連する問題