2013-03-27 113 views
-2

我想弄清楚如何让jQuery基于下拉菜单选择显示/隐藏动态生成的文本框。我的JS技能非常有限。我有以下的在while循环创建,取决于人的数量:基于下拉显示/隐藏动态文本框

<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" /> 

jQuery函数:

<script type='text/javascript'> 
$(window).load(function(){ 
    $('#location').change(function() { 
     if ($(this).val() == "Other") { 
      $('#location_other').show(); 
     } else { 
      $('#location_other').hide(); 
     } 
    }); 
}); 
</script> 

我需要保持的id和与$ PID变量序列化供以后处理。有没有另一种方法来调用这些jQuery的功能?

的HTML输出如下:

<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" /> 
+1

请出示PHP – castis 2013-03-27 00:49:01

+0

输出端的ID,您可以更改为'ID =“location_other ”' – 2013-03-27 00:51:54

+0

不 - 我需要保持它作为一个数组。否则,当我的PHP脚本处理它时,它会丢失信息。 – thebarless 2013-03-27 00:53:29

回答

0

尝试

$(window).load(function(){ 
    $('#location').change(function() { 
     $('input[id^=location_other]').hide(); 
     if ($(this).val() == "Other") { 
      $('#location_other').show(); 
     } else { 
      $('#location_other' + $(this).val()).hide(); 
     } 
    }); 
}); 
+0

这似乎没有任何影响。我一直玩弄它,因为我发现时间,但一直挂在什么“$('input [id^= location_other]')。hide();”正在完成。它不能预测location_other的数组值吗? – thebarless 2013-04-01 16:06:18

+0

而这段代码会显示我的循环的第一次迭代的location_other,下面的第N个循环会丢失它。 – thebarless 2013-04-01 16:12:53

0

试试这个

<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> 
+0

我无法得到这个工作:http://jsfiddle.net/thebarless/65NYu/ – thebarless 2013-04-01 17:06:20

+0

对不起,我忘了保存我的更改在jsfiddle中,请尝试$(document).ready()而不是$(窗口).load()。 http://jsfiddle.net/65NYu/2/ – 2013-04-02 04:28:31

+0

靠近!并感谢您的帮助。 http://jsfiddle.net/65NYu/您的脚本将使用两个下拉菜单显示和隐藏相同的文本框。我需要它来显示和隐藏两个单独的文本框。 – thebarless 2013-04-08 19:22:40

相关问题