简体   繁体   English

使用javascript更新select onchange事件上的文本框

[英]update text box on select onchange event using javascript

Hello i got a selection menu, i need to update 3 text boxes when the selection is done, and should load a default 1 as follows.. 您好我有一个选择菜单,我需要在选择完成后更新3个文本框,并应加载默认值1,如下所示。

    <select name="select" id="select">
      <option value="1">selection 1</option>
      <option value="2">selection 2</option>
      <option value="3">selection 3</option>
    </select>

    <input type="text" name="txt1" id="txt1" value="1"/>
    <input type="text" name="txt2" id="txt2" value="2"/>
    <input type="text" name="txt3" id="txt3" value="3"/>

// if selection 1 is selected text box's values should come as follows, (Default)
id="txt1" value="1" >> id="txt2" value="2" >> id="txt3" value="3"

// if selection 2 is selected text box's values should come as follows,
id="txt1" value="4" >> id="txt2" value="5" >> id="txt3" value="6"

// if selection 3 is selected text box's values should come as follows,
id="txt1" value="7" >> id="txt2" value="8" >> id="txt3" value="9"

any JavaScript help?? 任何JavaScript帮助? many thanks!!! 非常感谢!!!

I am a bit late, but here is it: 我有点晚了,但这是它:

$('select').change(function(){
  var sel=$(this).val();
    $('input[type=text]').each(function(i,el){
      $(el).val((i+1)+(sel-1)*3); 
    }); 
});

Edit : 编辑

Here is an example with a text input: http://jsfiddle.net/4LGWx/4/ 这是一个带文本输入的示例: http//jsfiddle.net/4LGWx/4/

t=['one','two','three','four','five','six','seven','eight','nine'];
$('select').change(function(){
  var sel=$(this).val();
    $('input[type=text]').each(function(i,el){
      $(el).val(t[i+(sel-1)*3]);
    });
});

or a slightly more elegant variant thereof: 或者稍微更优雅的变体:

$('select').change(function(){
  var t=[['one','two','three'],
         ['four','five','six'],
         ['seven','eight','nine']];
  var sel=$(this).val()-1;
    $('input[type=text]').each(function(i,el){
      $(el).val(t[i][sel]);
    });
});

(This time there is no global array ...) (这次没有全局数组...)

pure javascript implementation: 纯javascript实现:

(function (){
    var dd = document.getElementById('select');

    //get the text inputs into an array
    var inputs = ['txt1','txt2','txt3'].map(function(a){
      return document.getElementById(a);
    });

    //use an object to map the select values to the desired input values
    //this can be an object returned from your server or wherever.
    var inputValues = {'1':['abe','lincoln','president'],'2':['bob','dylan','song n dance man'],'3':['numbers',1,'two']};

    //bind the change event to the select 
    dd.onchange=function(e){

      //get the selected value of the drop down
      var selectedValue = dd.children[dd.selectedIndex].value;

      //change the values of the inputs
      for(var i = 0,j=inputs.length;i < j;i++){
        inputs[parseInt(i,10)].value = inputValues[selectedValue][i];
      }
    };
})();

JSBIN JSBIN

You can just monitor the select and make the changes from there: 您只需监控select并从那里进行更改:

var t1 = $('#txt1');
var t2 = $('#txt2');
var t3 = $('#txt3');

$('#select').on('change', function(e) {
    var v = $(this).val();

    if('1' === v) {
        t1.val('1');
        t2.val('2');
        t3.val('3');

    } else if('2' === v) {
        t1.val('4');
        t2.val('5');
        t3.val('6');

    } else if('3' === v) {
        t1.val('7');
        t2.val('8');
        t3.val('9');

    }
}

HTML: HTML:

<select name="select" id="select">
  <option value="1">selection 1</option>
  <option value="4">selection 2</option>
  <option value="7">selection 3</option>
</select>

<input type="text" name="txt1" id="txt1" value="1"/>
<input type="text" name="txt2" id="txt2" value="2"/>
<input type="text" name="txt3" id="txt3" value="3"/>

jQuery: jQuery的:

$(document).ready(function(){
  $('#select').change(function(){
    $select = $(this);
    $('input[id|="txt"]').each(function(i){
      $(this).val($select.val() + i);
    });
  });
});

Assign a class to your textboxes for easier manipulation: 为您的文本框分配一个类以便于操作:

<input type="text" name="txt1" id="txt1" value="1" class="txt"/>
<input type="text" name="txt2" id="txt2" value="2" class="txt"/>
<input type="text" name="txt3" id="txt3" value="3" class="txt"/>

JQuery: JQuery的:

$("#select").change(function(event){
    var selectedValue = parseInt($(this).val());
    $(".txt").each(function(){
        $(this).val(parseInt(this.defaultValue) + (selectedValue-1)*3);
    });
});

Notice the defaultValue . 注意defaultValue This is the value assigned in the html and does not change when you set the current value to another value. 这是在html中指定的值,并且在将当前值设置为其他值时不会更改。

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM