Html-我有两个多选框和一个标签

时间:2017-02-24 10:42:19

标签: javascript html css

当我选择两个值(每个多选框中有一个)时,我想更改标签的值。如box1中的option1和box2中的option2,标签值更改。

<select id="opt1" multiple="multiple">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
</select>

<select id="opt2" multiple="multiple">
    <option value="1">Opt 1</option>
    <option value="2">Opt 2</option>
    <option value="3">Opt 3</option>
    <option value="4">Opt 4</option>
</select><br />

<label id="changeme">This should change</label>

3 个答案:

答案 0 :(得分:0)

大致可能就是那样

.nav {}

您的最新样本JsFiddle

答案 1 :(得分:0)

var FLAG1=0;
function update(){
  if(FLAG1==0){
    FLAG1=1;
  }
  else{
    var ct=document.querySelectorAll('select');
    document.getElementById('changeme').innerHTML=ct[0].value+"-"+ct[1].value;
    FLAG1=0;
  }
}
<select id="opt1" multiple="multiple" onchange="update();">
        <option value="1">Option 1</option>
        <option value="2">Option 2</option>
    </select>

    <select id="opt2" multiple="multiple" onchange="update();">
        <option value="1">Opt 1</option>
        <option value="2">Opt 2</option>
        <option value="3">Opt 3</option>
        <option value="4">Opt 4</option>
    </select><br />

<label id="changeme">This should change</label>

答案 2 :(得分:0)

你可以从这样的事情开始

var label = document.getElementById("changeme");
var opt1 = document.getElementById("opt1");
var opt2 = document.getElementById("opt2");

opt1.onchange = function() {
  label.textContent = opt1.value + "-" + opt2.value;
}

opt2.onchange = function() {
  label.textContent = opt1.value + "-" + opt2.value;
}

https://jsfiddle.net/61ed0t6j/6/