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