0

为下拉选项列表的每个选项重复 php 块似乎很乏味,如本例所示:

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

有人可以帮我解决这个问题吗?这似乎令人困惑。编辑:具有函数 changeList 的 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 );
}
4

1 回答 1

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 的工作,除非您在选择时刷新页面。

于 2013-10-11T06:51:37.103 回答