jQuery-如何基于下拉列表中的值显示文本框
加载页面时,我希望隐藏jQuery-如何基于下拉列表中的值显示文本框,jquery,forms,show-hide,Jquery,Forms,Show Hide,加载页面时,我希望隐藏输入文本框,id为:amf-input-othertitle19 但是,如果用户从下拉列表中为上面的文本框选择某个值(“其他”),以便他们可以在该框中填写信息 下面是代码下拉列表: <select name="title_3" id="amf-input-title_3"> <option value="Title" id="title_3-0">Title</option> <option value="Mr" id="t
输入文本框
,id为:amf-input-othertitle19
但是,如果用户从下拉列表中为上面的文本框选择某个值(“其他”),以便他们可以在该框中填写信息
下面是代码下拉列表:
<select name="title_3" id="amf-input-title_3">
<option value="Title" id="title_3-0">Title</option>
<option value="Mr" id="title_3-1">Mr</option>
<option value="Mrs" id="title_3-2">Mrs</option>
<option value="Miss" id="title_3-3">Miss</option>
<option value="Ms" id="title_3-4">Ms</option>
<option value="Dr" id="title_3-5">Dr</option>
<option value="Professor" id="title_3-6">Professor</option>
<option value="Other" id="title_3-7">Other</option>
标题
先生
夫人
错过
太太
博士
教授
其他
要隐藏/显示的文本框:
<input type="text" class="text"
name="othertitle_19" id="amf-input-othertitle_19"
value="" placeholder="Please specify other...."
maxlength="255"
onkeyup="if (this.length>255) this.value=this.value.substr(0, 255)"
onblur="this.value=this.value.substr(0, 255)"
/>
您需要查看
选择的id,并在选择时显示它
$('document').ready(function() {
$('#amf-input-title_3').change(function() {
if($(this).val() == 'Other') {
$('#amf-input-othertitle_19').fadeIn();
}
else{
$('#amf-input-othertitle_19').fadeOut();
}
})
});
JavaScript:
// Shorthand for $(document).ready(function () { ... });
$(function () {
// It's more efficient to save off the jQuery object
// than to re-traverse the DOM each time.
var $input = $('#amf-input-othertitle_19');
// Catch the change on <select /> change
$('#amf-input-title_3').on('change', function () {
// Quick little ternary operator...
(this.value === "other") ? $input.show() : $input.hide();
});
});
// Make sure to hide the input on initial load...
#amf-input-othertitle_19 {
display: none;
}
// Shorthand for $(document).ready(function () { ... });
$(function () {
// It's more efficient to save off the jQuery object
// than to re-traverse the DOM each time.
var $input = $('#amf-input-othertitle_19');
// Catch the change on <select /> change
$('#amf-input-title_3').on('change', function () {
// Quick little ternary operator...
(this.value === "other") ? $input.show() : $input.hide();
});
});
// Make sure to hide the input on initial load...
#amf-input-othertitle_19 {
display: none;
}