2017-06-27 28 views
1

更新我試圖建立與選項相同的值(相同的文字)在2個下拉列表之間傳遞值。沒有點擊

選擇1個 選擇2個獨立的下拉選擇2

<script type="text/javascript"> 


jQuery(function ($) { 
var $set = $('#selector1, #selector2') 
$set.change(function() { 
    $set.not(this).val(this.value) 
}) 

}) 
</script> 

<html> 
<div style="float: right;margin-left: 10px;"> 
        <div > 
        <select id="selector1" onchange="" 
style="width:130px;"> 
         <option selected diabled hidden value="">Select Volume 
Type</option> 
         <option value="1">Auto-Weekly</option> 
         <option value="2">Auto-Monthly</option> 
         <option value="3">Custom Weekly</option> 
           <option value="4">Custom Monthly</option> 

        </select></div> 
       </div> 
<td style="text-align: center;"> 

<select id="selector2" style="width:80px;"> 
<option selected="" diabled="" hidden="" value="0">Select Report 
Period</option> 
         <option value="1">Auto-Weekly</option> 
         <option value="2">Auto-Monthly</option>  
          <option value="3">Custom Weekly</option> 
          <option value="4">Custom Monthly</option>   
        </select> 

          </td> 


</html> 

問題: 現在我已經使用上面的JavaScript匹配$ set的值。 問題是我必須'點擊'到下拉列表才能'更新'。 例如, 如果我在選擇器1中選擇了選項值= 1,那麼我必須'點擊'選擇器2才能看到更新的值。

如果還有更多精緻的解決方案,您可以請幫忙嗎?我稍後會以不同的方式使用這些選項,這就是爲什麼我需要2個獨立的選擇器並通過綁定來匹配它們。

+0

現在,您需要爲您更改選擇一個合適的值,以儘快更新選擇器2相同的值? –

+0

是的,目前要求我點擊'開始onchange抓住價值。 –

回答

0

我試過下面的腳本,但它仍然要求我點擊更新下拉值。

<script type="text/javascript"> 
$(document).ready(function(){ 
$('#selector1, #selector2').on('change',function() 
{ 
var report_answer = jQuery('#selector1').val(); 
var report_type = jQuery('#selector2').val(); 



var x= document.getElementById("selector1").selectedIndex; 
var y = document.getElementById("selector2").selectedIndex; 

    report_type = this.report_answer; 
    this.selectedIndex = y; 
     y=report_type; 
    return report_type; 

}); 
});