在 javascript 的输入数字类型字段中输入值时如何显示输入文本字段?
How to show a input text field when a value is entered in input number type field in javascript?
我正在处理一个表单,该表单有一个名为 amount 字段的字段,当数字大于 2000 时,一旦输入值,就会自动显示一个 pan card 字段,我尝试使用 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 lang="en">
<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>
<div class="col-lg-6 col-md-6 col-sm-12 form-group">
<label>Email Id*</label>
<input type="email" class="form-control" id="email" name="email" required/>
</div>
<div class="col-lg-12 col-md-12 col-sm-12 form-group">
<label>Amount (Rs.)*</label>
<input oninput="showPanfield()" type="number" class="form-control" id="amount" name="amount" required/>
</div>
<div class="col-lg-12 col-md-12 col-sm-12 form-group" id="pancarddiv">
<label>Pan Card No*</label>
<input type="text" class="form-control" id="panid" name="panid"/>
</div>
<div class="col-lg-12 col-md-12 col-sm-12 form-group">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
</div>
</div>
</body>
</html>
我希望有人能帮助我解决我落后的问题。
问题是因为你在函数外获取了 amount 字段的值。您只需要将 amnt
变量更改为金额元素,而不是其值:
var amnt=document.getElementById("amount");
var pandiv=document.getElementById("pancarddiv");
function showPanfield(){
if(amnt.value >= 2000){
pandiv.style.display="block";
}
else{
pandiv.style.display="none";
}
}
<!doctype html>
<html lang="en">
<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>
<div class="col-lg-6 col-md-6 col-sm-12 form-group">
<label>Email Id*</label>
<input type="email" class="form-control" id="email" name="email" required/>
</div>
<div class="col-lg-12 col-md-12 col-sm-12 form-group">
<label>Amount (Rs.)*</label>
<input oninput="showPanfield()" type="number" class="form-control" id="amount" name="amount" required/>
</div>
<div class="col-lg-12 col-md-12 col-sm-12 form-group" id="pancarddiv">
<label>Pan Card No*</label>
<input type="text" class="form-control" id="panid" name="panid"/>
</div>
<div class="col-lg-12 col-md-12 col-sm-12 form-group">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
</div>
</div>
</body>
</html>
此代码有两个问题
document.getElementById("amount").value
returns 一个字符串,所以如果你希望得到一个数字,你应该把它转换成一个数字(例如使用 unary + operator)
在 if
语句中,您尝试仍然访问 amnt
变量的不存在的 value
属性字符串而不是对象
所以要解决这两个问题,请将语句更改为
if (+amnt >= 2000) {
...
}
附带说明一下,如果您使用内联事件处理程序,则不应使用括号,因为您正在分配函数 oninput="showPanfield"
我正在处理一个表单,该表单有一个名为 amount 字段的字段,当数字大于 2000 时,一旦输入值,就会自动显示一个 pan card 字段,我尝试使用 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 lang="en">
<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>
<div class="col-lg-6 col-md-6 col-sm-12 form-group">
<label>Email Id*</label>
<input type="email" class="form-control" id="email" name="email" required/>
</div>
<div class="col-lg-12 col-md-12 col-sm-12 form-group">
<label>Amount (Rs.)*</label>
<input oninput="showPanfield()" type="number" class="form-control" id="amount" name="amount" required/>
</div>
<div class="col-lg-12 col-md-12 col-sm-12 form-group" id="pancarddiv">
<label>Pan Card No*</label>
<input type="text" class="form-control" id="panid" name="panid"/>
</div>
<div class="col-lg-12 col-md-12 col-sm-12 form-group">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
</div>
</div>
</body>
</html>
我希望有人能帮助我解决我落后的问题。
问题是因为你在函数外获取了 amount 字段的值。您只需要将 amnt
变量更改为金额元素,而不是其值:
var amnt=document.getElementById("amount");
var pandiv=document.getElementById("pancarddiv");
function showPanfield(){
if(amnt.value >= 2000){
pandiv.style.display="block";
}
else{
pandiv.style.display="none";
}
}
<!doctype html>
<html lang="en">
<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>
<div class="col-lg-6 col-md-6 col-sm-12 form-group">
<label>Email Id*</label>
<input type="email" class="form-control" id="email" name="email" required/>
</div>
<div class="col-lg-12 col-md-12 col-sm-12 form-group">
<label>Amount (Rs.)*</label>
<input oninput="showPanfield()" type="number" class="form-control" id="amount" name="amount" required/>
</div>
<div class="col-lg-12 col-md-12 col-sm-12 form-group" id="pancarddiv">
<label>Pan Card No*</label>
<input type="text" class="form-control" id="panid" name="panid"/>
</div>
<div class="col-lg-12 col-md-12 col-sm-12 form-group">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
</div>
</div>
</body>
</html>
此代码有两个问题
document.getElementById("amount").value
returns 一个字符串,所以如果你希望得到一个数字,你应该把它转换成一个数字(例如使用 unary + operator)在
if
语句中,您尝试仍然访问amnt
变量的不存在的value
属性字符串而不是对象
所以要解决这两个问题,请将语句更改为
if (+amnt >= 2000) {
...
}
附带说明一下,如果您使用内联事件处理程序,则不应使用括号,因为您正在分配函数 oninput="showPanfield"