2017-08-17 84 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(); 
     } 
    }); 
});