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

标签 jquery select input

我有选择和一些输入(范围+文本)。当我选择选择之一时,我需要执行输入获取值的操作,但代码仅适用于第一个选择。当我改变我的选择值时,它不会改变。我应该纠正什么?

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

关于jQuery 如果选择选项标签 = ... 设置输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45733721/

相关文章:

jquery - 使用 jQuery 实际设置选项元素上的 selected 属性

html - Mobile Safari 多选错误

java - 鼠标事件加剧问题

javascript - 在 javascript 中从表单中换行输入文本

javascript - 如何调整两个不同大小的不同模态

javascript - 2 列 bootstrap div - 左边应该固定在顶部,右边应该是可滚动的

javascript - HTML5 文件 API 使用 Ajax 上传多个图像

sql-server - 我可以将 select 语句中的 select 转换为自连接吗?

python - 动态 python 用户输入到单独的 C 程序

php - $.ajax 无法处理 php 脚本中的错误