jQuery Show a Textbox When an Option is Selected
I have the following code:
<select>
<option value="Type 1">Type 1</option>
<option value="Type 2">Type 2</option>
<option value="Type 3">Type 3</option>
<option value="Oth开发者_StackOverflower">Other</option>
</select>
<input type="text" id="other" />
What I want to do is using jQuery make the textbox below hidden by default, and then show it if a user selects the other option from the dropdown.
No need for any css here.
$('#sel').change(function() {
var selected = $(this).val();
if(selected == 'Other'){
$('#other').show();
}
else{
$('#other').hide();
}
});
<select id="sel">
<option value="Type 1">Type 1</option>
<option value="Type 2">Type 2</option>
<option value="Type 3">Type 3</option>
<option value="Other">Other</option>
</select>
<input type="text" id="other" style="display: none;" />
$('#sel').change(function() {
$('#other').css('display', ($(this).val() == 'Other') ? 'block' : 'none');
});
Try this:
<select id="selectbox_id">
<option value="Type 1">Type 1</option>
<option value="Type 2">Type 2</option>
<option value="Type 3">Type 3</option>
<option value="Other">Other</option>
</select>
<input type="text" id="other" />
JQuery:
$(function(){
// hide by default
$('other').css('display', 'none');
$('selectbox_id').change(function(){
if ($(this).val() === 'Other') {
$('other').css('display', 'block');
}
});
});
<select id = "ddlPassport" onchange = "ShowHideDiv()" style="width:150px;">
<option value="Y">Type 1</option>
<option value="Y">Type 2</option>
<option value="N">Type 3</option>
<option value="D">Other</option>
</select>
<script type="text/javascript">
function ShowHideDiv()
{
var ddlPassport = document.getElementById("ddlPassport");
var dvPassport = document.getElementById("dvPassport");
dvPassport.style.display = ddlPassport.value == "Y" ? "block" : "none";
dvPassport1.style.display = ddlPassport.value == "N" ? "block" : "none";
dvPassport2.style.display = ddlPassport.value == "D" ? "block" : "none";
}
<div id="dvPassport" style="display: none">
<form action="viewdata.php" method="POST">
Enter Data:
<input type="text" id="txtPassportNumber" name="data" />
<br>
<input type="submit" value="Search" name="submit"
style="float:right;width:150px;margin-top:10px;">
</form>
</div>
<div id="dvPassport1" style="display: none">
<form action="viewdata.php" method="POST">
Enter Data:
<input type="number" id="txtPassportNumber" name="data" />
<br>
<input type="submit" value="Search" name="submit"
style="float:right;width:150px;margin-top:10px;">
</form>
</div>
<div id="dvPassport2" style="display: none">
<form action="viewdata.php" method="POST">
Enter Data:
<input type="date" id="txtPassportNumber" name="data" />
<br>
<input type="submit" value="Search" name="submit"
style="float:right;width:150px;margin-top:10px;">
</form>
</div>
精彩评论