Javascript 如何根据选择值显示表单输入字段?
我知道这很简单,我需要在谷歌搜索。我尽了最大努力,却找不到更好的解决办法。我有一个表单字段,它接受一些输入,还有一个选择字段,它有一些值。它还具有“其他”价值 我想要的是: 如果用户选择“其他”选项,则会显示一个文本字段,指定应显示“其他”。当用户选择另一个选项(而不是“其他”)时,我想再次隐藏它。我如何使用JQuery执行该操作 这是我的JSP代码Javascript 如何根据选择值显示表单输入字段?,javascript,jquery,html,jsp,Javascript,Jquery,Html,Jsp,我知道这很简单,我需要在谷歌搜索。我尽了最大努力,却找不到更好的解决办法。我有一个表单字段,它接受一些输入,还有一个选择字段,它有一些值。它还具有“其他”价值 我想要的是: 如果用户选择“其他”选项,则会显示一个文本字段,指定应显示“其他”。当用户选择另一个选项(而不是“其他”)时,我想再次隐藏它。我如何使用JQuery执行该操作 这是我的JSP代码 <label for="db">Choose type</label> <select name="dbType"
<label for="db">Choose type</label>
<select name="dbType" id=dbType">
<option>Choose Database Type</option>
<option value="oracle">Oracle</option>
<option value="mssql">MS SQL</option>
<option value="mysql">MySQL</option>
<option value="other">Other</option>
</select>
<div id="otherType" style="display:none;">
<label for="specify">Specify</label>
<input type="text" name="specify" placeholder="Specify Databse Type"/>
</div>
选择类型
您必须使用val()
而不是value()
,并且您错过了起始引号id=dbType“
应该是id=“dbType”
$(document).ready(function () {
toggleFields(); // call this first so we start out with the correct visibility depending on the selected form values
// this will call our toggleFields function every time the selection value of our other field changes
$("#dbType").change(function () {
toggleFields();
});
});
// this toggles the visibility of other server
function toggleFields() {
if ($("#dbType").val() === "other")
$("#otherServer").show();
else
$("#otherServer").hide();
}
改变
到
或
您的代码有一些问题:
您在select元素的id上缺少一个打开的引号,因此:我得到了它的答案。这是我的代码
<label for="db">Choose type</label>
<select name="dbType" id=dbType">
<option>Choose Database Type</option>
<option value="oracle">Oracle</option>
<option value="mssql">MS SQL</option>
<option value="mysql">MySQL</option>
<option value="other">Other</option>
</select>
<div id="other" class="selectDBType" style="display:none;">
<label for="specify">Specify</label>
<input type="text" name="specify" placeholder="Specify Databse Type"/>
</div>
$(document).ready(function () {
toggleFields(); // call this first so we start out with the correct visibility depending on the selected form values
// this will call our toggleFields function every time the selection value of our other field changes
$("#dbType").change(function () {
toggleFields();
});
});
// this toggles the visibility of other server
function toggleFields() {
if ($("#dbType").val() === "other")
$("#otherServer").show();
else
$("#otherServer").hide();
}
HTML:
选择类型
服务器:
选择数据库类型
神谕
MS SQL
MySQL
其他
服务器:
端口:
函数myfun(){
$(文档).ready(函数(){
$(“#选择”)。单击(
函数(){
var数据=$(“#选择”).val();
$(“#disp”).val(数据);
});
});
}
身份证
第一
第二
我仍然无法获取。我在问,这个问题是否与此有关?
您能提供联机JQuery链接吗?我需要检查它是否有效。谢谢此解决方案也很有效:
selection = this.value;
$('#dbType').change(function(){
var selection = $(this).val();
if(selection == 'other')
{
$('#otherType').show();
}
else
{
$('#otherType').hide();
}
});
$('#dbType').on('change',function(){
if( $(this).val()==="other"){
$("#otherType").show()
}
else{
$("#otherType").hide()
}
});
$('#dbType').on('change',function(){
var selection = $(this).val();
switch(selection){
case "other":
$("#otherType").show()
break;
default:
$("#otherType").hide()
}
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
<label for="db">Choose type</label>
<select name="dbType" id=dbType">
<option>Choose Database Type</option>
<option value="oracle">Oracle</option>
<option value="mssql">MS SQL</option>
<option value="mysql">MySQL</option>
<option value="other">Other</option>
</select>
<div id="other" class="selectDBType" style="display:none;">
<label for="specify">Specify</label>
<input type="text" name="specify" placeholder="Specify Databse Type"/>
</div>
$(function() {
$('#dbType').change(function() {
$('.selectDBType').slideUp("slow");
$('#' + $(this).val()).slideDown("slow");
});
});
$(document).ready(function () {
toggleFields(); // call this first so we start out with the correct visibility depending on the selected form values
// this will call our toggleFields function every time the selection value of our other field changes
$("#dbType").change(function () {
toggleFields();
});
});
// this toggles the visibility of other server
function toggleFields() {
if ($("#dbType").val() === "other")
$("#otherServer").show();
else
$("#otherServer").hide();
}
<p>Choose type</p>
<p>Server:
<select id="dbType" name="dbType">
<option>Choose Database Type</option>
<option value="oracle">Oracle</option>
<option value="mssql">MS SQL</option>
<option value="mysql">MySQL</option>
<option value="other">Other</option>
</select>
</p>
<div id="otherServer">
<p>Server:
<input type="text" name="server_name" />
</p>
<p>Port:
<input type="text" name="port_no" />
</p>
</div>
<p align="center">
<input type="submit" value="Submit!" />
</p>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function myfun(){
$(document).ready(function(){
$("#select").click(
function(){
var data=$("#select").val();
$("#disp").val(data);
});
});
}
</script>
</head>
<body>
<p>id <input type="text" name="user" id="disp"></p>
<select id="select" onclick="myfun()">
<option name="1"value="1">first</option>
<option name="2"value="2">second</option>
</select>
</body>
</html>