我有两个onchange方法和两个我需要传递的值

时间:2014-02-21 03:12:47

标签: javascript html

function changeHiddenInput(cLeague, nLeague) {
        console.log(cLeague);
        console.log(nLeague);
        var objHidden1 = document.getElementById("hiddenInput1");
        var objHidden2 = document.getElementById("hiddenInput2");
        objHidden1.value = cLeague.value;
        objHidden2.value = nLeague.value;
        var a = objHidden1.value;
        var b = objHidden1.value;
        result.innerHTML = a + b;
    }

<select class="form-control" id="currentleague" onchange="document.getElementById('currentleague').src=this.value; changeHiddenInput(select)">
    <option value="rankicons/bronze5.png" (another value goes somewhere in here)>Bronze V</option>
</select>

基本上第一个onchange会更改图像的值,第二个onchange传递一个值并进行一些数学运算。有没有我可以使用的替代值,或者我可以以某种方式传递两个值,并以某种方式告诉他们分开?

1 个答案:

答案 0 :(得分:0)

您可以在value属性中包含2个值:

<option value="rankicons/bronze5.png,value nr 2">...</option>

样品:

function changeHiddenInput(league) {
        var league_values = league.split(",");
        var objHidden1 = document.getElementById("hiddenInput1");
        var objHidden2 = document.getElementById("hiddenInput2");
        objHidden1.value = league_values[0];
        objHidden2.value = league_values[1];
        var a = objHidden1.value;
        var b = objHidden1.value;
        result.innerHTML = a + b;
        // remove comment and set uniqe id to set img element src
        //document.getElementById('img_id').src=league_values[0];
    }

HTML:

<select class="form-control" id="currentleague" onchange="changeHiddenInput(this.options[this.selectedIndex].value)">
    <option value="rankicons/bronze5.png,value nr 2">...</option>
</select>

我删除了值document.getElementById('currentleague').src=this.value;,因为它引用了select元素本身(相同的id)并将其添加到您的函数中。