下拉第一个选项值不起作用
Dropdown first option value not functional
我的下拉列表有问题,
当使用值选项属性选择选项时,我需要更改位置页面,但第一个选项元素不起作用,
这是 html 代码:
<form>
<select class='language-select' name='URL' id='URL'>
<option value="index.php?lan=EN" selected> English</option>
<option value="index.php?lan=FR">Français</option>
</select>
</form>
这是Js函数:
$('#URL').on('change', function(event){
event.preventDefault();
event.stopPropagation();
var $this = $(this);
window.location.href = $(this).val();
alert($(this).val());
});
还有一个错误“未捕获的异常:内存不足”
有一件事,当我添加一个空选项元素时效果很好,我可以在两个链接之间切换:index.php?lan=FR 和 /index.php?lan=EN
谢谢
无论如何,您的 html 都会将第一个 option
标记为 selected
。因此,例如,当加载法语页面时,您的下拉菜单仍然显示 "English"。相反,您需要做的是在页面加载时检测应该选择哪个选项。您可以使用这样的函数:
function GetURLParameter(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
您的标记为:
<form>
<select class='language-select' name='URL' id='URL'>
<option id="EN" value="index.php?lan=EN"> English</option>
<option id="FR" value="index.php?lan=FR">Français</option>
</select>
</form>
最后,在您的 jQuery on ready 函数中,您将拥有:
$(function() {
var lan = GetURLParameter('lan');
$('#' + lan).prop('selected', true);
});
希望对您有所帮助!
我的下拉列表有问题,
当使用值选项属性选择选项时,我需要更改位置页面,但第一个选项元素不起作用, 这是 html 代码:
<form>
<select class='language-select' name='URL' id='URL'>
<option value="index.php?lan=EN" selected> English</option>
<option value="index.php?lan=FR">Français</option>
</select>
</form>
这是Js函数:
$('#URL').on('change', function(event){
event.preventDefault();
event.stopPropagation();
var $this = $(this);
window.location.href = $(this).val();
alert($(this).val());
});
还有一个错误“未捕获的异常:内存不足”
有一件事,当我添加一个空选项元素时效果很好,我可以在两个链接之间切换:index.php?lan=FR 和 /index.php?lan=EN
谢谢
无论如何,您的 html 都会将第一个 option
标记为 selected
。因此,例如,当加载法语页面时,您的下拉菜单仍然显示 "English"。相反,您需要做的是在页面加载时检测应该选择哪个选项。您可以使用这样的函数:
function GetURLParameter(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
您的标记为:
<form>
<select class='language-select' name='URL' id='URL'>
<option id="EN" value="index.php?lan=EN"> English</option>
<option id="FR" value="index.php?lan=FR">Français</option>
</select>
</form>
最后,在您的 jQuery on ready 函数中,您将拥有:
$(function() {
var lan = GetURLParameter('lan');
$('#' + lan).prop('selected', true);
});
希望对您有所帮助!