2013-10-11 43 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的工作,除非你被選擇後刷新頁面。

相關問題