如何设置“已选择”选项'选择下拉列表国家和州的

时间:2014-09-03 01:56:22

标签: javascript jquery mysql wordpress drop-down-menu

我的过滤搜索流程:

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管理员,我想要一个国家用于Ex USA State:纽约,默认情况下。

1 个答案:

答案 0 :(得分:0)

如果您所使用的值是应该的值,则需要在每个循环上进行测试 “选择”。

// 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>';
}