2017-08-17 104 views
2

我有選擇和一些輸入(範圍+文本)。我需要做的是當我選擇一個選擇,輸入獲取值,但代碼只適用於第一選擇。當我改變我的選擇的價值並沒有改變。我應該糾正什麼?jQuery如果選擇選項標籤= ...設置輸入值

$(document).ready(function() {  
    $("div.roword select").change(function() { 
     var text = $(this).find("option:selected").text(); 
     if (text = "60x90") { 
      $("input#height, input#heightPlus").attr('value', '60'); 
      $("input#width, input#widthPlus").attr('value', '90'); 
      $("input#height, input#width").focus(); 
      $("input#height, input#width").blur(); 
      } else 
     if (text = "100x150") { 
      $("input#height, input#heightPlus").attr('value', '100'); 
      $("input#width, input#widthPlus").attr('value', '150'); 
      $("input#height, input#width").focus(); 
      $("input#height, input#width").blur(); 
      } else 
     if (text = "120x180") { 
      $("input#height, input#heightPlus").attr('value', '120'); 
      $("input#width, input#widthPlus").attr('value', '180'); 
      $("input#height, input#width").focus(); 
      $("input#height, input#width").blur(); 
      } 
    }); 
}); 
+3

使用''== /''===比較運算,而不是'='賦值運算符中,如果塊即'文本=== 「100x150」'並設置值使用'.val()'方法,即'$(「input#width,input#widthPlus」).val(90)' – Satpal

回答

2

轉換: -

if (text = "60x90") { 

要: -

if (text == "60x90") { //or if (text === "60x90") { 

等等,對別人

因爲=賦值運算符沒有比較運算符

變化

$("input#height, input#heightPlus").attr('value', '60'); 

要: -

$("input#height, input#heightPlus").val(60); 

所以,對其他attr('value')也......

完整代碼必須是這樣的: -

$(document).ready(function() {  
    $("div.roword select").change(function() { 
     var text = $(this).find("option:selected").text(); 
     if (text == "60x90") { 
      $("input#height, input#heightPlus").val(60); 
      $("input#width, input#widthPlus").val(90); 
      $("input#height, input#width").focus(); 
      $("input#height, input#width").blur(); 
     } 
     else if (text == "100x150") { 
      $("input#height, input#heightPlus").val(100); 
      $("input#width, input#widthPlus").val(150); 
      $("input#height, input#width").focus(); 
      $("input#height, input#width").blur(); 
     } 
     else if(text == "120x180") { 
      $("input#height, input#heightPlus").val(120); 
      $("input#width, input#widthPlus").val(180); 
      $("input#height, input#width").focus(); 
      $("input#height, input#width").blur(); 
     } 
    }); 
});