Javascript 使用java脚本设置HTML属性值
我有html下拉列表,其中有国家列表。现在我想将当前国家设置为列表的默认值。我找到了使用地理位置获取国家的JavaScript代码 我的代码:Javascript 使用java脚本设置HTML属性值,javascript,html,Javascript,Html,我有html下拉列表,其中有国家列表。现在我想将当前国家设置为列表的默认值。我找到了使用地理位置获取国家的JavaScript代码 我的代码: function getCountry(var name) { if(name==geoip_country_name()) { return "selected"; } } 然后我需要设置选项列表的selected属性 我试过这个: <option value="Sri Lanka" selected="getC
function getCountry(var name) {
if(name==geoip_country_name()) {
return "selected";
}
}
然后我需要设置选项列表的selected属性
我试过这个:
<option value="Sri Lanka" selected="getCountry('sri Lanka')">Sri Lanka</option>
但这是不对的。
基本上,我想使用JavaScript函数设置所选属性值
如何做到这一点?如果您使用的是JQuery,那么下面这行应该可以解决您的问题:
$('select').val(geoip_country_name());
如果geoip\u country\u name以小写形式返回名称,则在初始化选择列表时,每个选项的值都应以小写形式返回。如果您使用JQuery,则以下行应该可以解决您的问题:
$('select').val(geoip_country_name());
如果geoip\u country\u name以小写形式返回名称,则在初始化选择列表时,每个选项的值均以小写形式显示。基本上,您可以这样做:
<html>
<body>
<select id="country">
<option id="germany" value="Germany">DE</option>
<option id="uk" value="UK">UK</option>
<option id="usa" value="USA">USA</option>
</select>
<script>
var selectedCountry = 'uk'; //getCountry
var index = document.getElementById(selectedCountry).index;
document.getElementById("country").selectedIndex=index;
</script>
渲染选择后启动脚本
请注意,此示例可能不是最佳实践。我也不确定它是否适用于所有浏览器。您可以使用适当的框架,如JQuery、Mootools等,基本上您可以这样做:
<html>
<body>
<select id="country">
<option id="germany" value="Germany">DE</option>
<option id="uk" value="UK">UK</option>
<option id="usa" value="USA">USA</option>
</select>
<script>
var selectedCountry = 'uk'; //getCountry
var index = document.getElementById(selectedCountry).index;
document.getElementById("country").selectedIndex=index;
</script>
渲染选择后启动脚本
请注意,此示例可能不是最佳实践。我也不确定它是否适用于所有浏览器。您可以使用适当的框架,如JQuery、Mootools、所选属性不会自动计算为JS代码。假设您已在变量country中存储了所需的国家名称,可以尝试以下操作:
var country = "Sri Lanka";
var select = document.getElementById('myselect'); //Change to the ID of your select element
for (var i = 0; i < select.options.length; i++){
if(select.options[i].value == country)
select.selectedIndex = i;
}
所选属性不会自动计算为JS代码。假设您已在变量country中存储了所需的国家名称,可以尝试以下操作:
var country = "Sri Lanka";
var select = document.getElementById('myselect'); //Change to the ID of your select element
for (var i = 0; i < select.options.length; i++){
if(select.options[i].value == country)
select.selectedIndex = i;
}
使用window.onload事件,只需设置下拉列表的值。请记住,您的硬编码国家名称可能与地理信息服务不同
<script>
window.onload = function(){
document.getElementById("country").value = geoip_country_name();
}
</script>
<select id="country" name="country">
<option value="Sri Lanka">Sri Lanka</option>
<option value="UK">UK</option>
<option value="USA">USA</option>
<select>
使用window.onload事件,只需设置下拉列表的值。请记住,您的硬编码国家名称可能与地理信息服务不同
<script>
window.onload = function(){
document.getElementById("country").value = geoip_country_name();
}
</script>
<select id="country" name="country">
<option value="Sri Lanka">Sri Lanka</option>
<option value="UK">UK</option>
<option value="USA">USA</option>
<select>
你说乔纳森是什么意思?@JonathandeM。如果OP只想设置一次默认值,为什么他们需要监听更改事件?另外,他们在这里没有使用jQuery。你是什么意思乔纳森?@JonathandeM。如果OP只想设置一次默认值,为什么他们需要监听更改事件?此外,他们在这里没有使用jQuery。