从属性中选择适当的值

selecting proper value from attribute

本文关键字:选择 从属性      更新时间:2023-09-26

我有一个选择框用于选择国家代码:-

<div class="selectWrap">
    <select name="countryCode" id="countryCode"></select>
</div>

它使用javascript填充,并获得如下选项:-

<option data-phone-code="+91">91&nbsp(India)</option>

现在我只想选择+91的值。

I was trying this

var country = document.getElementById("countryCode");
var prefix = country.options[country.selectedIndex].value;

但是这会返回带有Country的Full值。

使用getAttribute,例如:

country.options[country.selectedIndex].getAttribute('data-phone-code');