如何在下拉列表中选择默认选项?

时间:2014-09-12 14:42:51

标签: javascript php cakephp

我有这个JS文件,默认情况下我想选择一个选项。我的意思是,我想选择国家" USA"价值" USA" 。
这是我默认拥有的:

enter image description here

这是JS文件:

var town_country_arr = new Array("Canada", "USA");    
    var s_a = new Array();        
    s_a[0]="";        
    s_a[1]="Alberta|British Columbia|Manitoba|New Brunswick|Newfoundland|Northwest Territories|Nova Scotia|Nunavut|Ontario|Prince Edward Island|Quebec|Saskatchewan|Yukon Territory";

    s_a[2]="Alabama|Alaska|Arizona|Arkansas|California|Colorado|Connecticut|Delaware|District of Columbia|Florida|Georgia|Hawaii|Idaho|Illinois|Indiana|Iowa|Kansas|Kentucky|Louisiana|Maine|Maryland|Massachusetts|Michigan|Minnesota|Mississippi|Missouri|Montana|Nebraska|Nevada|New Hampshire|New Jersey|New Mexico|New York|North Carolina|North Dakota|Ohio|Oklahoma|Oregon|Pennsylvania|Rhode Island|South Carolina|South Dakota|Tennessee|Texas|Utah|Vermont|Virginia|Washington|West Virginia|Wisconsin|Wyoming";

    function print_town_country(town_country_id){        
        // given the id of the <select> tag as function argument, it inserts <option> tags        
        var option_str = document.getElementById(town_country_id);        
        option_str.length=0;        
        option_str.options[0] = new Option('Pays','');        
        option_str.selectedIndex = 0;        
        for (var i=0; i<town_country_arr.length; i++) {        
            option_str.options[option_str.length] = new Option(town_country_arr[i],town_country_arr[i]);

        }

    }

编辑:我在Cake中的观点

<div class="form-group">
                          <div class="col-xs-6">
                            <?php echo $this->Form->input('country', array('type' => 'select', 'class' => 'form-control', 'label' => 'Pays', 'id' => 'town_country', 'onchange' => "print_state('state',this.selectedIndex);", 'selected' => $selectedcountry, 'empty' => 'Pays')); ?>
                          </div>
                      </div>

                      <div class="form-group">
                          <div class="col-xs-6">
                      <?php 
                      echo $this->Form->input('localisation', array('type' => 'select', 'class' => 'form-control', 'label' => 'Localisation', 'id' => 'state', 'empty' => $namelocalisation,
                            'options' => $localisations)); ?>
                          </div>
                      </div>  

                      <script language="javascript">print_town_country("town_country");</script>    

变量selectedcountry等于USA ...

使用Firebug:

<div class="col-xs-6">
<div class="input select required">
<label for="town_country">Pays</label>
<select id="town_country" class="form-control" onchange="print_state('state',this.selectedIndex);" name="data[Town][country]">
<option value="">Pays</option>
<option value="Canada">Canada</option>
<option value="USA">USA</option>
</select>
</div>
</div>

2 个答案:

答案 0 :(得分:1)

您可能不需要Javascript。你可以简单地说。

<select>

<option value="Pays">Pays</option>
<option value="Canada">Canada</option>
<option value="USA" selected>USA</option> //This will be the default selected value
<option></option>

</select>

换句话说,您只需在默认情况下要选择的选项上添加select 属性

对于原生javascript,请使用setAttribute,如下所示:

.setAttribute("selected","selected")

如果你喜欢jQuery,你可以这样做:

$('select option:eq(2)').prop('selected', true); 

答案 1 :(得分:0)

有些人滥用selected - 属性作为默认选择。不幸的是,没有default - 属性这样的东西,但是:第一个元素是通过标准惯例选择的。

<select>中设置您想要的选项作为默认值。