如何简化此Javascript函数?
我试图扩展它,但在创建JS函数时,我有点生疏。我独自一人走了这么远,但唉 当一个范围得到一个值,在这种情况下,一个城市名称,我希望选择框自动匹配它。我想通过去掉所有这些,来扩大规模如何简化此Javascript函数?,javascript,jquery,Javascript,Jquery,我试图扩展它,但在创建JS函数时,我有点生疏。我独自一人走了这么远,但唉 当一个范围得到一个值,在这种情况下,一个城市名称,我希望选择框自动匹配它。我想通过去掉所有这些,来扩大规模 $(document).ready(function() { $('select#field1 option').removeAttr('selected'); var whereEvent = $('span#field2').html(); if (whereEvent == 'New York')
$(document).ready(function() {
$('select#field1 option').removeAttr('selected');
var whereEvent = $('span#field2').html();
if (whereEvent == 'New York') {
$('select#field1 option[value=New York]').attr('selected', 'true');
} else if (whereEvent == 'Los Angeles') {
$('select#field1 option[value=Los Angeles]').attr('selected', 'true');
} else if (whereEvent == 'Tokyo') {
$('select#field1 option[value=Tokyo]').attr('selected', 'true');
} else if (whereEvent == 'London') {
$('select#field1 option[value=London]').attr('selected', 'true');
} else if (whereEvent == 'Sydney') {
$('select#field1 option[value=Sydney]').attr('selected', 'true');
} else if (whereEvent == 'Paris') {
$('select#field1 option[value=Paris]').attr('selected', 'true');
}
});
有人能帮我吗?我保证我会感谢你的帮助。谢谢。也许:
$('select#field1 option').removeAttr('selected');
var whereEvent = $('span#field2').html();
$('select#field1 option[value='+whereEvent+']').attr('selected', 'true');
?也许:
$('select#field1 option').removeAttr('selected');
var whereEvent = $('span#field2').html();
$('select#field1 option[value='+whereEvent+']').attr('selected', 'true');
?我不知道jQuery,所以可能有一种更优雅的方式,但是:
var whereEvent = $('span#field2').html();
$('select#field1 option[value=' + whereEvent + ']').attr('selected', 'true');
我不知道jQuery,所以可能有一种更优雅的方式,但是:
var whereEvent = $('span#field2').html();
$('select#field1 option[value=' + whereEvent + ']').attr('selected', 'true');
这是一个选项:
$(document).ready(function() {
$('select#field1 option').removeAttr('selected');
var whereEvent = $('span#field2').html();
var filter = 'select#field1 option[value=' + whereEvent + ']';
$(filter).attr('selected', 'true');
}
或者你可以:
$(document).ready(function() {
var filter = $('span#field2').html();
$('select#field1 option').each(function(){
this.selected = (this.value == filter);
});
}
这是一个选项:
$(document).ready(function() {
$('select#field1 option').removeAttr('selected');
var whereEvent = $('span#field2').html();
var filter = 'select#field1 option[value=' + whereEvent + ']';
$(filter).attr('selected', 'true');
}
或者你可以:
$(document).ready(function() {
var filter = $('span#field2').html();
$('select#field1 option').each(function(){
this.selected = (this.value == filter);
});
}
这至少会起作用,而且看起来也是合乎逻辑的
$(document)
.ready(function () {
$("select#field1 option")
.removeAttr("selected");
var a = $("span#field2")
.html();
a == "New York" ? $("select#field1 option[value=New York]")
.attr("selected", "true") : a == "Los Angeles" ? $("select#field1 option[value=Los Angeles]")
.attr("selected", "true") : a == "Tokyo" ? $("select#field1 option[value=Tokyo]")
.attr("selected", "true") : a == "London" ? $("select#field1 option[value=London]")
.attr("selected", "true") : a == "Sydney" ? $("select#field1 option[value=Sydney]")
.attr("selected", "true") : a == "Paris" && $("select#field1 option[value=Paris]")
.attr("selected", "true")
})
这至少会起作用,而且看起来也是合乎逻辑的
$(document)
.ready(function () {
$("select#field1 option")
.removeAttr("selected");
var a = $("span#field2")
.html();
a == "New York" ? $("select#field1 option[value=New York]")
.attr("selected", "true") : a == "Los Angeles" ? $("select#field1 option[value=Los Angeles]")
.attr("selected", "true") : a == "Tokyo" ? $("select#field1 option[value=Tokyo]")
.attr("selected", "true") : a == "London" ? $("select#field1 option[value=London]")
.attr("selected", "true") : a == "Sydney" ? $("select#field1 option[value=Sydney]")
.attr("selected", "true") : a == "Paris" && $("select#field1 option[value=Paris]")
.attr("selected", "true")
})
这就是我困惑的地方。”这里不需要“如果”。非常感谢。这就是我困惑的地方。”这里不需要“如果”。非常感谢。您也不需要删除TTR,设置一个选定的选项将取消选择以前选定的选项。您也不需要删除TTR,设置一个选定的选项将取消选择以前选定的选项。您不应该为此使用attr和removeAttr-您需要的是.prop'selected',true/false您不应该为此使用attr和removeAttr-您需要的是.prop'selected',true/false