2017-02-24 68 views
-1

當我選擇兩個值(每個多選框中有一個)時,我想更改標籤的值。像box1中的option1和box2中的option2以及標籤值更改。Html-我有兩個多選框和一個標籤

<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> 

回答

0

大致可能是這樣的

var s1_t = 'N/A'; 
var s2_t = 'N/A'; 

$('#opt1').change(function(){ 
    var $this = $(this); 
    var val = $this.val(); 
    s1_t = val ? $this.find("option[value=" + val + "]").text() : 'N/A'; 
    $('#changeme').text(s1_t + ' - ' + s2_t) 
}); 

$('#opt2').change(function(){ 
    var $this = $(this); 
    var val = $this.val(); 
    s2_t = val ? $this.find("option[value=" + val + "]").text() : 'N/A'; 
    $('#changeme').text(s1_t + ' - ' + s2_t) 
}); 

你更新的樣本JsFiddle

+0

謝謝您的答覆。我真正想要的是:opt1和opt2是位置,當我選擇兩個選項(每個選項)時,標籤將顯示它們之間的距離。任何幫助,將不勝感激 – Falak

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>

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/