如果条件为 true,则在输入字段中打印具有特定 ID 的文本

If condition is true print text in input field with specific ID

本文关键字:打印 文本 ID 字段 true 条件 输入 如果      更新时间:2023-09-26

菜鸟在这里需要一些输入。我现在花了几个小时试图让它工作,用PHP和javascript,这就是我现在所处的位置。

我想获取一个输入字段以根据条件显示特定文本。如果输入字段"ebctotal"中的数字在 1-4 范围内,则在输入字段"hue"中显示文本"非常苍白"。

法典:

function getHue() {
var ebc = document.getElementById("ebctotal").value;
if (ebc >= 1 && ebc <= 4) {
    // insert text "Very pale" into element with id 'hue' 
document.getElementById('hue').value;
}
}

.HTML:

// print text in this field
<input class="input" type="text" id="hue" size="7" maxlength="20">
// based on value of this field
<input class="input" type="text" id="ebctotal" size="7" maxlength="20">

我走在正确的轨道上吗?

干杯

如果你想检查"ebc"的长度是否在 1-4 之间,那么试试喜欢;

function getHue() {
   var ebc = document.getElementById("ebctotal").value;
   if (ebc.length>=1 && ebc.length<=4) {
       document.getElementById('hue').value='Very pale';
    }
}

或者"ebc"的值是否在 1-4 之间,然后尝试喜欢;

function getHue() {
   var ebc = parseInt(document.getElementById("ebctotal").value);
   if (ebc>=1 && ebc<=4) {
       document.getElementById('hue').value='Very pale';
    }
}