2013-05-21 4 views
0

드롭 다운 목록의 첫 번째 항목을 비워두고 기본적으로 선택하도록 할 수 있습니까?하지만 드롭 다운 목록에서 항목을 선택하고 제출을 누르면 해당 값을 그대로 유지하십시오.옵션 표시가 비어있는 경우 계속 값 유지 선택 제출

여기에 내가 다시 빈에 선택된 값을 변경합니다 제출 명중 현재 경우 예를 들어

<form action="" method="post" /> 
<SELECT NAME="whatever" id="whatever"> 
<?php $selected = isset($_POST["whatever"]) && $_POST["whatever"] == 'whatever'; ?> 
<option value="" hidden="true" selected="selected"></option> 
<OPTION VALUE=1 onclick"selected='selected'">All</OPTION> 
</SELECT> 
<input type="submit" name="submit" value="Update"/> 
</form> 

입니다.

+0

비어있는 선택 표시 및 숨기기 + 집중할 때 전체 선택 표시 ... – mplungjan

답변

0

유 내가이 도움이 희망이

<form action="" method="post" /> 
<SELECT NAME="whatever" id="whatever"> 
<option value="">Select value</option> 
<OPTION VALUE=1 <?php if($_POST["whatever"]==1) echo "selected='selected'";?> >All</OPTION> 
<OPTION VALUE=2 <?php if($_POST["whatever"]==2) echo "selected='selected'";?> >Value One</OPTION> 
</SELECT> 
<input type="submit" name="submit" value="Update"/> 
</form> 
+0

감사합니다.이 웹 사이트는 많은 도움을주었습니다. – user2403705

0
function html_select($name, $options, $selected= "") 
{ 
    // open <select> tag, set id & name attributes 
    $select = "<select id=\"$name\" name=\"$name\">\n"; 

    // populate options to drop down list 
    foreach ($options as $value=> $html) { 
     $select .= "<option value=\"$value\""; 

     // select the selected option 
     if ($value == $selected) { 
      $select .= " selected=\"selected\""; 
     } 

     $select .= ">$html</option>\n"; 
    } 

    // close </select> tag 
    $select .= "</select>"; 

    // return element 
    return $select ; 
} 

echo html_select("whatever", array("" => "Select Value", "1" => "Value 1"), $_POST["whatever"]); 

처럼 사용할 수 있습니다.

관련 문제