【问题标题】:jQuery If slect option label = ... set input valuejQuery If select option label = ... 设置输入值
【发布时间】:2017-08-17 11:19:04
【问题描述】:

我有选择和一些输入(范围 + 文本)。当我选择一个选择时,我需要做,输入获取值但代码仅适用于第一选择。当我改变我的选择值并没有改变。我应该更正什么?

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

【问题讨论】:

  • 在 if 块中使用 ==/=== 比较运算符而不是 = 赋值运算符,即 text === "100x150" 并设置值使用 .val() 方法,即 $("input#width, input#widthPlus").val(90)

标签: jquery select input


【解决方案1】:

转换:-

if (text = "60x90") {

收件人:-

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

其他人的等等

因为 =assignment-operator 而不是 comparison-operator

还有

改变

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 2016-03-23
    • 2016-04-26
    • 2013-08-25
    • 1970-01-01
    • 2018-06-20
    • 2011-04-30
    相关资源
    最近更新 更多