我正在尝试使用jQuery更改下拉选择框中的所选选项。我已经设置它,以便找到URL末尾的哈希标记,并基于该哈希标记更改选择的选项。
我的大部分代码都是功能性的,它成功地找到了哈希标记,并执行与之相对应的if语句。但是,当它执行语句的“then”部分时,当它转到选项的选择器(它使用基于选项标记的value属性的属性选择器)时,它返回null。如果在firebug中找到这个错误,在控制台中它会显示选择器为null。
以下是我的代码:
所以,为了澄清,当我输入以#php散列标记结尾的网址时,例如,所需操作不会发生,这将使用“selected”html属性将“PHP编码”选项更改为所选选项,但特定选项标签的选择器返回null。我的语法有问题还是我的代码没有按照我认为的方式运行?非常感谢。
我的大部分代码都是功能性的,它成功地找到了哈希标记,并执行与之相对应的if语句。但是,当它执行语句的“then”部分时,当它转到选项的选择器(它使用基于选项标记的value属性的属性选择器)时,它返回null。如果在firebug中找到这个错误,在控制台中它会显示选择器为null。
以下是我的代码:
$(document).ready(function() {
var $hash = window.location.hash
if($hash == "#htmlcss") {
$('option[value="HTML/CSS Coding"]').attr("selected","selected")
}
if($hash == "#php") {
$('option[value="PHP Coding"]').attr("selected","selected")
}
if($hash == "#jscript") {
$('option[value="Javascript and jQuery Coding"]').attr("selected","selected")
}
if($hash == "#improv") {
$('option[value="General Website Improvements"]').attr("selected","selected")
}
if($hash == "#towp") {
$('option[value="Website Conversion to Wordpress"]').attr("selected","selected")
}
if($hash == "#wptheme") {
$('option[value="Wordpress Theme Design"]').attr("selected","selected")
}
if($hash == "#complete") {
$('option[value="Complete Website Creation"]').attr("selected","selected")
}
if($hash == "#server") {
$('option[value="Web Server Configuration"]').attr("selected","selected")
}
});
所以,为了澄清,当我输入以#php散列标记结尾的网址时,例如,所需操作不会发生,这将使用“selected”html属性将“PHP编码”选项更改为所选选项,但特定选项标签的选择器返回null。我的语法有问题还是我的代码没有按照我认为的方式运行?非常感谢。