基于其他文本字段自动填充文本字段

时间:2018-02-06 10:17:32

标签: javascript php html

  

的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的数据。

HTML Form. I've attached the image link, if you see the image of the form my problem will be much more clear

提前致谢。

3 个答案:

答案 0 :(得分:0)

你的代码搞砸了!

您的第一个错误就在这里:

if(pname == "Olive" || "olive")

此条件始终为True!要测试pname是否等于Oliveolive,您的If语句应为:

if(pname == "Olive" || pname == "olive") {

此代码段可以使用。

&#13;
&#13;
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;
&#13;
&#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)

&#13;
&#13;
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;
&#13;
&#13;