簡體   English   中英

如何設置選擇下拉列表國家和州的“選擇的選項”

[英]How to set the 'selected option' of a select dropdown list country and state

我的過濾器搜索流程:

case 'region_state':
$output .= '
<div class="clear"></div>
<label><st>' . __( "Country", AT_TEXTDOMAIN ) . ':</s2></label>
<div class="select_box_1">
    <select name="region_id" id="region_id" class="custom-select select_1">';
    $output .= '<option value="0">' . __( "All", AT_TEXTDOMAIN ) . '</option>';
    foreach ($view->add_widget( 'reference_widget', array( 'method' => 'get_regions' ), true ) as $key => $value) {
        $output .= '<option value="' . $value['id'] . '">' . $value['name'] . '</option>';
      }
    // foreach (AT_VC_Helper::get_manufacturers() as $value => $key) {
    //  $output .= '<option value="' . $key. '">' . $value . '</option>';
    // }
    $output .= '
    </select>
    </div>
    <label><s3>' . __( "State", AT_TEXTDOMAIN ) . ':</s3></label>
    <div class="select_box_1">
      <select name="state_id" id="state_id" class="custom-select select_1">
        <option>All</option>
      </select>
    </div>';
    $output .= '<input type="submit" value="' . __( "Search", AT_TEXTDOMAIN ) . '" class="btn_search btn-1 float-right" id="search_car_shortcode1"/>';
            break;

數據來自reference_widget WordPress管理員,我希望有1個國家用於美國前州:紐約,默認情況下。

您需要在每個循環上進行測試,如果您使用的值是應“選擇”的值。

// the value you want to be selected
$region_id = $_REQUEST['region_id'];
// loop through the options
foreach ( $view->add_widget( 'reference_widget', array( 'method' => 'get_regions' ), true ) as $key => $value ){
    // if the value matches, we want to output "selected"
    $selected = ( $region_id == $value )? ' selected' : '';
    $output .= '<option value="' . $value['id'] . '"' . $selected . '>' . $value['name'] . '</option>';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM