的JavaScript
function changeInput() {
var pname = document.getElementById("pname").value;
if(pname == "Olive" || "olive") {
var remark = "150, Sinker-Single Jersey, Micro Polyester-Dry Fit";
}
else if(pname == "Umber" || "umber") {
var remark = "180, Matte - Mesh, Micro Polyester-Dry Fit";
}
}
document.getElementById("remark").value = remark;
var pname = document.getElementById("pname");
pname.onchange = function() {
changeInput();
}
pname.onkeyup = function() {
changeInput();
}
HTML
<input type="text" name="pname" id="pname" value="" placeholder="Product Name">
<input type="text" name="remark" id="remark" value="" placeholder="Product Name">
如果我尝试在第一个输入字段中键入输入Olive,则它必须自动填充下一个输入字段。 我面临的问题是,如果尝试输入Umber,那么它也会在输入字段中填充Olive的数据。
提前致谢。
答案 0 :(得分:0)
你的代码搞砸了!
您的第一个错误就在这里:
if(pname == "Olive" || "olive")
此条件始终为True!要测试pname
是否等于Olive
或olive
,您的If
语句应为:
if(pname == "Olive" || pname == "olive") {
此代码段可以使用。
function changeInput() {
var pname = document.getElementById("pname").value;
var remark = "";
if(pname == "Olive" || pname == "olive") {
remark = "150, Sinker-Single Jersey, Micro Polyester-Dry Fit";
document.getElementById("remark").value = remark;
}
else if(pname == "Umber" || pname == "umber") {
remark = "180, Matte - Mesh, Micro Polyester-Dry Fit";
document.getElementById("remark").value = remark;
}
else{
document.getElementById("remark").value = "";
}
}
var pname = document.getElementById("pname");
pname.onkeyup = function() {
changeInput();
}
&#13;
<input type="text" name="pname" id="pname" value="" placeholder="Product Name">
<input type="text" name="remark" id="remark" value="" placeholder="Product Name">
&#13;
答案 1 :(得分:0)
使用此 -
<script>
var pname = document.getElementById("pname").value;
function changeInput() {
var remark;
if(pname == "Olive" || "olive") {
remark = "150, Sinker-Single Jersey, Micro Polyester-Dry Fit";
}
if(pname == "Umber" || "umber") {
remark = "180, Matte - Mesh, Micro Polyester-Dry Fit";
}
console.log("remark:"+remark);
document.getElementById("remark").value = remark;
}
</script>
和HTML -
<input type="text" name="pname" id="pname" value="" onchange ="changeInput()" placeholder="Product Name">
<input type="text" name="remark" id="remark" value="" placeholder="Product Name">
答案 2 :(得分:0)
var pname = document.getElementById("pname")
var remark = document.getElementById("remark");
pname.addEventListener('keyup',function(){
if(pname.value == "Olive" || pname.value == "olive") {
remark.value = "150, Sinker-Single Jersey, Micro Polyester-Dry Fit";
}
else if(pname.value == "Umber" || pname.value == "umber" ) {
remark.value = "180, Matte - Mesh, Micro Polyester-Dry Fit";
} else{
remark.value = "";
}
})
&#13;
<input type="text" name="pname" id="pname" value="" placeholder="Product Name">
<input type="text" name="remark" id="remark" value="" placeholder="Product Name">
&#13;