jQuery如果选择选项label = ...设置输入值

时间:2017-08-17 11:19:04

标签: jquery select input

我有选择和一些输入(范围+文本)。当我选择select,input get值之一时,我需要这样做,但代码仅适用于第一选择。当我改变我的选择值时,没有改变。我应该纠正什么?

$(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();
            }
    });
});

1 个答案:

答案 0 :(得分: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();
        }
    });
});
相关问题