Php 基于下拉列表显示/隐藏动态文本框
我试图弄清楚如何让jQuery根据下拉菜单选择显示/隐藏动态生成的文本框。我的JS技能非常有限。我在while循环中创建了以下内容,具体取决于人数:Php 基于下拉列表显示/隐藏动态文本框,php,jquery,Php,Jquery,我试图弄清楚如何让jQuery根据下拉菜单选择显示/隐藏动态生成的文本框。我的JS技能非常有限。我在while循环中创建了以下内容,具体取决于人数: <select name="location[<?=$pid?>]" id="location[<?=$pid?>]"> <option value="<?=$loc_id?>" selected><?=$loc_name?> </option> <optio
<select name="location[<?=$pid?>]" id="location[<?=$pid?>]">
<option value="<?=$loc_id?>" selected><?=$loc_name?> </option>
<option value="Other">Other</option>
<option value="Other">--------</option>
<? $query_loc = mysqli_query($link, "SELECT * FROM locations WHERE loc_app = 'Y' ORDER BY loc_name ASC");
while($fetch_loc = mysqli_fetch_array($query_loc)){
$loc_id = $fetch_loc['loc_id'];
$loc_name = $fetch_loc['loc_name'];
?>
<option value="<?=$loc_id?>"><?=$loc_name?></option>
<?
} ?>
</select>
<input name="location_other[<?=$pid?>]" type="text" id="location_other[<?=$pid?>]" style="display:none" />
试一试
试试这个
<select name="location[287]" id="location[287]">
<option value="Other">Other</option>
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
</select>
<input name="location_other[287]" type="text" id="location_other" style="display:none" />
<script type='text/javascript'>
$(window).load(function() {
if ($('select[id^="location"]').val() == "Other") {
$('#location_other').show();
} else {
$('#location_other').hide();
}
$('select[id^="location"]').change(function() {
if ($(this).val() == "Other") {
$('#location_other').show();
} else {
$('#location_other').hide();
}
});
});
</script>
其他
一个
两个
三
$(窗口)。加载(函数(){
if($('select[id^=“location”]”)。val()=“Other”){
$(“#位置_其他”).show();
}否则{
$(“#位置_其他”).hide();
}
$('select[id^=“location”]”)。更改(函数(){
如果($(this.val()=“其他”){
$(“#位置_其他”).show();
}否则{
$(“#位置_其他”).hide();
}
});
});
请显示phpcan的输出,您可以将id更改为id=“location\u other”
否-我需要将其保留为一个数组。否则,当我的php脚本处理它时,它会丢失信息think@Arun约翰尼,我被纠正了-看起来这很有效。现在我该如何让兽皮发挥作用呢?这似乎没有任何效果。我一直在断断续续地玩弄它,因为我找到了时间,但总是挂断了“$('input[id^=location\u other]”)。hide();”正在完成的任务。我无法预测location\u other的数组值,这有什么区别吗?虽然此代码将在我的循环的第一次迭代中显示location\u other,但下面的第n个循环将丢失它。很抱歉,我忘记在JSFIDLE中保存我的更改,请尝试$(document).ready()而不是$(window).load()。越来越近了!谢谢你的帮助。脚本将显示和隐藏具有两个下拉菜单的同一文本框。我需要它来显示和隐藏两个单独的文本框。将下拉列表和文本框包装到容器中,然后重试。
<select name="location[287]" id="location[287]">
<option value="1" selected>A</option>
<option value="Other">Other</option>
<option value="Other">--------</option>
<option value="12">B</option>
<option value="12">C</option>
</select>
<input name="location_other[287]" type="text" id="location_other[287]" style="display:none" />
$(window).load(function(){
$('#location').change(function () {
$('input[id^=location_other]').hide();
if ($(this).val() == "Other") {
$('#location_other').show();
} else {
$('#location_other' + $(this).val()).hide();
}
});
});
<select name="location[287]" id="location[287]">
<option value="Other">Other</option>
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
</select>
<input name="location_other[287]" type="text" id="location_other" style="display:none" />
<script type='text/javascript'>
$(window).load(function() {
if ($('select[id^="location"]').val() == "Other") {
$('#location_other').show();
} else {
$('#location_other').hide();
}
$('select[id^="location"]').change(function() {
if ($(this).val() == "Other") {
$('#location_other').show();
} else {
$('#location_other').hide();
}
});
});
</script>