在javascript中的输入数字类型字段中输入值时如何显示输入文本字段?

我正在开发一个表单,其中有一个名为金额字段的字段,当数字大于 2000 时,一旦输入值,就会自动显示平移卡字段,我尝试使用 javascript,但最终结果是这样代码如下:


    var amnt=document.getElementById("amount").value;

    var pandiv=document.getElementById("pancarddiv");

    function showPanfield(){

        if(amnt.value >= 2000){

            pandiv.style.display="block";

        }

        else{

            pandiv.style.display="none";

        }

    }

<!doctype html>

<html>

<head>

<title>Donation Form</title>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="css/bootstrap.min.css"/>

<script src="js/bootstrap.min.js"></script>

<script src="js/jquery.min.js"></script>

<style>

    div#pancarddiv {

        display: none;

}

</style>

</head>

<body>

    <div class="container-fluid">

        <div class="row">

            <form action="" method="POST">

                <div class="col-lg-12 col-md-12 col-sm-12 form-group">

                    <label>Full Name*</label>

                    <input type="text" class="form-control" id="name" name="fname" required/>

                </div>

                <div class="col-lg-12 col-md-12 col-sm-12 form-group">

                    <label>Postal Address*</label>

                    <textarea class="form-control" rows="4" id="Address" name="address" required></textarea>

                </div>

                <div class="col-lg-6 col-md-6 col-sm-12 form-group">

                    <label>Mobile No.*</label>

                    <input type="number" class="form-control" id="mobileno" name="mobile" required/>

                </div>

我希望有人能帮助我解决我落后的问题。




芜湖不芜
浏览 61回答 2
2回答

慕斯王

问题是因为一旦在函数之外您就获得了 amount 字段的值。您只需将amnt变量更改为 amount 元素,而不是其值:&nbsp; &nbsp; var amnt=document.getElementById("amount");&nbsp; &nbsp; var pandiv=document.getElementById("pancarddiv");&nbsp; &nbsp; function showPanfield(){&nbsp; &nbsp; &nbsp; &nbsp; if(amnt.value >= 2000){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pandiv.style.display="block";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pandiv.style.display="none";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }<!doctype html><html><head><title>Donation Form</title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="css/bootstrap.min.css"/><script src="js/bootstrap.min.js"></script><script src="js/jquery.min.js"></script><style>&nbsp; &nbsp; div#pancarddiv {&nbsp; &nbsp; &nbsp; &nbsp; display: none;}</style></head><body>&nbsp; &nbsp; <div class="container-fluid">&nbsp; &nbsp; &nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <form action="" method="POST">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-lg-12 col-md-12 col-sm-12 form-group">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Full Name*</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" class="form-control" id="name" name="fname" required/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-lg-12 col-md-12 col-sm-12 form-group">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Postal Address*</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <textarea class="form-control" rows="4" id="Address" name="address" required></textarea>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-lg-6 col-md-6 col-sm-12 form-group">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Mobile No.*</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="number" class="form-control" id="mobileno" name="mobile" required/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-lg-6 col-md-6 col-sm-12 form-group">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Email Id*</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="email" class="form-control" id="email" name="email" required/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-lg-12 col-md-12 col-sm-12 form-group">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Amount (Rs.)*</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input oninput="showPanfield()" type="number" class="form-control" id="amount" name="amount" required/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-lg-12 col-md-12 col-sm-12 form-group" id="pancarddiv">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Pan Card No*</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" class="form-control" id="panid" name="panid"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-lg-12 col-md-12 col-sm-12 form-group">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="submit" class="btn btn-default">Submit</button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; </body></html>

白板的微信

这段代码有两个问题document.getElementById("amount").value返回一个字符串,因此如果您希望获得一个数字,您应该将其转换为数字(例如使用一元 + 运算符)在if语句中,您尝试仍然访问变量value的不存在属性amnt,该属性是字符串而不是对象因此,为了解决这两个问题,请将语句更改为if (+amnt >= 2000) {    ... }作为旁注,如果您使用内联事件处理程序,则不应使用括号,因为您正在分配函数oninput="showPanfield"
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5