从下拉列表中选择时更改2个值

时间:2014-07-01 12:35:22

标签: javascript forms

这是我的表格,我需要做的是,

当用户选择" Eyes"时," lm_key"和" lm_form"字段获得" 5750"和#34; cc0ce4fe280e46e986e5716f9feedaab"值。

当用户选择" Ears"时," lm_key"和" lm_form"领域获得" 5455"和#34; b50ec1fe282a46e986efd8795aedcec"值。

   <form method="post" action="http://api.leadmanager.co.il/v1/submit" id="lm_form" name="form"> 
     <input type="hidden" name="lm_form" value="5750" /> 
     <input type="hidden" name="lm_key" value="cc0ce4fe280e46e986e5716f9feedaab" /> 
     <input type="hidden" name="lm_tyt" value="" />
     <select name="selectField" onchange="changeValues()">
         <option value="1">Eyes</value>
         <option value="2">Ears</value>
     </select>
     <input type="submit" value="submit"/>
     </form>

我需要添加什么样的jaavscript才能在选择时一次更改2个值?

感谢

2 个答案:

答案 0 :(得分:0)

    funchition changeValues ( e ) {

Im_key = document.getElementsByName('Im_key')[0];
Im_form = document.getElementsByName('Im_form')[0];

if( e.target.value == 1 ) {

Im_key.value = 'foobar1';
Im_form.value = 'foobar2'


} else if( e.target.value == 1 ) {

Im_key.value = 'foobar3';
Im_form.value = 'foobar4'

}

}

根据您的需要改变foobar

答案 1 :(得分:0)

你可以这样做:(纯javascript,多变量用于理解发生了什么)

function changeValues(){
    var values = ['', 5750, 5455]; // first key is 0
    var keys = ['', 'cc0ce4fe280e46e986e5716f9feedaab', 'b50ec1fe282a46e986efd8795aedcec']; // first key is 0
    var myform = document.forms['form'];
    var lm_form = myform.lm_form;
    var lm_key = myform.lm_key;
    var selected = myform.selectField.value;
    myform.lm_form.value = values[selected];
    myform.lm_key.value = keys[selected];
}

JSFiddle

简短版本:

var values = ['', 5750, 5455]; // first key is 0
var keys = ['', 'cc0ce4fe280e46e986e5716f9feedaab', 'b50ec1fe282a46e986efd8795aedcec']; // first key is 0
function changeValues(){
    document.forms['form'].lm_form.value = values[document.forms['form'].selectField.value];
    document.forms['form'].lm_key.value = keys[document.forms['form'].selectField.value];
}