select標籤類比placeholder屬性與一般操作(最重要的是ios某一項被選中的相容)

來源:互聯網
上載者:User

標籤:選中   ext   style   opacity   操作   ast   _id   select   hold   

1.為了統一樣式,常常要類比placeholder

<select>

    <option disabled selected hidden>請選擇</option>

    <option>what</option>

    <option>the</option>

    <option>hell</option>

</select>

2.select選中的值和某個值要被選中(ios相容)

<select style="font-size: 60px">
<option>2</option>
<option>5年</option>
<option>10年</option>
<option>20年</option>
</select>
<select style="font-size: 50px" id="select_id ">
<option value="1">年交</option>
<option value="2">躉交</option>
</select>

a 取得選中值:

擷取select 選中的 text :
    $("selecr").eq(0).find("option:selected").text();

擷取select選中的 value:
      $("selecr").eq(0).val();

擷取select選中的索引:
      $("selecr")[0].selectedindex;

b 設定select

var iText= $("selecr").eq(0).find("option:selected").text();

$(‘select‘).eq(1).find("option[value = ‘"+iText+"‘]").prop("selected","selected");

之前用的是attr,但是發現ios怎麼都不行,沒想到只要把attr換成prop就沒有相容問題了,ios真是磨人的小妖精啊!

還有$(‘select‘).eq(1).val(iText);
    $(‘select‘).eq(1).get(0).value =iText;

下面這倆種沒有做相容測試,按道理是沒有問題的因為是修改之前的代碼,不敢大改

c 清空 select:
    $(‘select‘).eq(1).empty();

d 增加或減少select的opacity項

$‘(#select_id‘).append("<option value=‘value‘>text</option>");  //添加一項option
$(#select_id‘).prepend("<option value=‘0‘>請選擇</option>"); //在前面插入一項option
    $("#select_id option:last").remove(); //刪除索引值最大的option
    $("#select_id option[index=‘0‘]").remove();//刪除索引值為0的option
    $("#select_id option[value=‘3‘]").remove(); //刪除值為3的option
    $("#select_id option[text=‘4‘]").remove(); //刪除text值為4的option

select標籤類比placeholder屬性與一般操作(最重要的是ios某一項被選中的相容)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.