我没有完全得到下面的代码,但我在下面有一个下拉菜单,我希望它在下面显示这些值:
1
1/2
2
2/3
3
3/4
4
4/5
...
10
现在我还希望能够在提交后保持选择的选项,所以我在下面尝试了这段代码,但问题是它只是显示值1/2, 2/3, 3/4, 4/5...
等。
foreach ($years as $year) {
if ($validSubmission && $year == $getduration) {
if ($year != $max_year) {
$nextYear = $year + 1;
$durationHTML .= "<option value='" . $year . "' selected='selected'>$year/$nextYear</option>".PHP_EOL;
}else{
$durationHTML .= "<option value='" . $year . "' selected='selected'>$year</option>".PHP_EOL;
}
}else{
if ($year != $max_year) {
$nextYear = $year + 1;
$durationHTML .= "<option value='" . $year . "'>$year/$nextYear</option>".PHP_EOL;
}else{
$durationHTML .= "<option value='" . $year . "'>$year</option>" . PHP_EOL;
}
}
}
原始代码如下所示,它显示了正确的选项,但没有执行 $validSubmission 变量,因此在提交页面后它不会保持选择的选项:
foreach ($years as $year) {
$durationHTML .= "<option>$year</option>".PHP_EOL;
if ($year != $max_year) {
$nextYear = $year + 1;
$durationHTML .= "<option>$year/$nextYear</option>".PHP_EOL;
}
}
$durationHTML .= '</select>';
下面的代码确实在提交后保持选项显示,但不显示/
介于值之间的值:
foreach ($years as $year) {
if ($validSubmission && $year == $getduration) {
$durationHTML .= "<option value='" . $year . "' selected='selected'>$year</option>" . PHP_EOL;
} else {
$durationHTML .= "<option value='" . $year . "'>$year</option>" . PHP_EOL;
}
}
但试图结合这两个代码没有奏效,这就是我遇到的问题