2013-10-11 39 views
0

PHP块似乎单调乏味重复的PHP块的下拉选项列表中的每一个选项,因为在这种情况下:环路周围选择HTML选项,并添加每个

<select name="religion" id="relaff" onchange="changeList(this)"> 
    <option <?php if($religion == "Paganism"){echo " selected=\"Paganism\"";} ?> value="Paganism">Paganism</option> 
    <option <?php if($religion == "Wicca"){echo " selected=\"Wicca\"";} ?> value="Wicca">Wicca</option> 
    <option <?php if($religion == "Witchcraft"){echo " selected=\"Witchcraft\"";} ?> value="Witchcraft">Witchcraft</option> 
    <option <?php if($religion == "Islam"){echo " selected=\"Islam\"";} ?> value="Islam">Islam</option> 
    <option <?php if($religion == "Buddhism"){echo " selected=\"Buddhism\"";} ?> value="Buddhism">Buddhism</option> 
    <option <?php if($religion == "No religion"){echo " selected=\"No religion\"";} ?> value="No religion">No religion</option> 
    <option <?php if($religion == "Christianity"){echo " selected=\"Christianity\"";} ?> value="Christianity">Christianity</option> 
    <option <?php if($religion == "Other"){echo " selected=\"Other\"";} ?> value="Other">Other</option></select> 

这是因为在最坏的情况下同样的形式,我有另一个下拉菜单,根据在上面编码的下拉列表中选择的内容自动生成一个值。这里是第二个下拉菜单相关的上述之一:

<select name="denomination" id="reldet"> 
<option 
<?php echo " selected=" . $denomination . ""; ?> 
value="NONE">This will update after selecting a religion</option></select> 

有人可以帮我解决这个问题吗?这似乎令人困惑。 编辑:具有功能的变更表的JS文件是在这里:

var lists = new Array(); 

lists['Select religion'] = new Array(); 
lists['Select religion'][0] = new Array(
'This will update after selecting a religion' 
); 
lists['Select religion'][1] = new Array(
'' 
); 

// First set of text and values 
lists['Paganism'] = new Array(); 
lists['Paganism'][0] = new Array(
'Hellenic', 
'Asatru', 
'Roman', 
'Celtic' 
); 
lists['Paganism'][1] = new Array(
'Hellenic', 
'Asatru', 
'Roman', 
'Celtic' 
); 

// Second set of text and values 
lists['Wicca'] = new Array(); 
lists['Wicca'][0] = new Array(
'Gardnerian', 
'Eclectic', 
'Inclusive', 
'Solitary' 
); 
lists['Wicca'][1] = new Array(
'Gardnerian', 
'Eclectic', 
'Inclusive', 
'Solitary' 
); 

lists['Witchcraft'] = new Array(); 
lists['Witchcraft'][0] = new Array(
'British Traditional', 
'Santeria', 
'Voodoo' 
); 
lists['Witchcraft'][1] = new Array(
'British Traditional', 
'Santeria', 
'Voodoo' 
); 

lists['Christianity'] = new Array(); 
lists['Christianity'][0] = new Array(
'Catholic', 
'Aglipayan', 
'Protestant', 
'Pentecostal', 
'Baptist', 
'Methodist', 
'Iglesia Ni Cristo', 
'Latter-Day Saints (Mormons)', 
'Seventh-Day Adventist', 
// string escape method here 
'Jehovah\'s Witnesses' 
); 
lists['Christianity'][1] = new Array(
'Catholic', 
'Aglipayan', 
'Protestant', 
'Pentecostal', 
'Baptist', 
'Methodist', 
'Iglesia Ni Cristo', 
'Latter-Day Saints (Mormons)', 
'Seventh-Day Adventist', 
// string escape method here 
'Jehovah\'s Witnesses' 
); 

lists['Other'] = new Array(); 
lists['Other'][0] = new Array(
'Judaism', 
'Sikhism', 
'Shintoism' 
); 
lists['Other'][1] = new Array(
'Judaism', 
'Sikhism', 
'Shintoism' 
); 

lists['No religion'] = new Array(); 
lists['No religion'][0] = new Array(
'Atheist', 
'Agnostic' 
); 
lists['No religion'][1] = new Array(
'Atheist', 
'Agnostic' 
); 

// This function goes through the options for the given 
// drop down box and removes them in preparation for 
// a new set of values 

function emptyList(box) { 
// Set each option to null thus removing it 
while (box.options.length) box.options[0] = null; 
} 

// This function assigns new drop down options to the given 
// drop down box from the list of lists specified 

function fillList(box, arr) { 
// arr[0] holds the display text 
// arr[1] are the values 

for (i = 0; i < arr[0].length; i++) { 

    // Create a new drop down option with the 
    // display text and value from arr 

    option = new Option(arr[0][i], arr[1][i]); 

    // Add to the end of the existing options 

    box.options[box.length] = option; 
} 

// Preselect option 0 

box.selectedIndex=0; 
} 

// This function performs a drop down list option change by first 
// emptying the existing option list and then assigning a new set 

function changeList(box) { 
// Isolate the appropriate list by using the value 
// of the currently selected option 

list = lists[box.options[box.selectedIndex].value]; 

// Next empty the slave list 

emptyList(box.form.denomination); 

// Then assign the new list values 

fillList(box.form.denomination, list); 
} 
+0

首先查看代码中选定的属性,并将其与http://www.w3schools.com/tags/tag_option.asp上的文档进行比较。提示:选定的属性只有一个“选定”作为值。 changeList()javascript函数是什么?你能提供任何代码吗? 第二个下拉菜单中的php代码令人困惑。 <?php标记之前的$面值是什么? – Marcel

+0

我刚刚添加了JavaScript代码。 $面额是一个错字。我会解决这个问题。 – kureidothephpsniper

回答

1

有很多方法来避免繁琐的工作。其中之一可能是:

<?php 
$options = array('Paganism', 'Wicca', ...); 
?> 

<select name="religion" id="relaff" onchange="changeList(this)"> 
    <?php 
    foreach($options as $option){ 
    ?> 
    <option <?php if($religion == $option){echo " selected=\"$option\"";} ?> value="<?php echo $option;?>"><?php echo $option ; ?></option> 

    <?php } ?> 


</select> 

然而,要改变另一个下拉列表的选项时,作出选择,我认为这是一个JavaScript的工作,除非你被选择后刷新页面。

相关问题