【发布时间】:2010-09-14 01:20:30
【问题描述】:
使用 jQuery,如何检查选择菜单中是否有一个选项被选中,如果没有,将其中一个选项指定为选中。
(选择是在我刚刚继承的应用程序中使用迷宫般的 PHP 函数生成的,所以这是一个快速修复,当我了解这些时:)
【问题讨论】:
标签: javascript jquery forms dom html-select
使用 jQuery,如何检查选择菜单中是否有一个选项被选中,如果没有,将其中一个选项指定为选中。
(选择是在我刚刚继承的应用程序中使用迷宫般的 PHP 函数生成的,所以这是一个快速修复,当我了解这些时:)
【问题讨论】:
标签: javascript jquery forms dom html-select
如果您需要显式检查每个选项以查看是否有任何具有“选定”属性的选项,您可以这样做。否则使用option:selected,您将获得第一个默认选项的值。
var viewport_selected = false;
$('#viewport option').each(function() {
if ($(this).attr("selected") == "selected") {
viewport_selected = true;
}
});
【讨论】:
我找到了一个很好的方法来检查是否选择了选项并在未选择时选择默认值。
if(!$('#some_select option[selected="selected"]').val()) {
//here code if it HAS NOT selected value
//for exaple adding the first value as "placeholder"
$('#some_select option:first-child').before('<option disabled selected>Wybierz:</option>');
}
如果 #some_select 没有默认选择选项,则 .val() 为 未定义
【讨论】:
虽然我不确定你想要完成什么,但这段代码对我有用。
<select id="mySelect" multiple="multiple">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
<option value="4">Fourth</option>
</select>
<script type="text/javascript">
$(document).ready(function() {
if (!$("#mySelect option:selected").length) {
$("#mySelect option[value='3']").attr('selected', 'selected');
}
});
</script>
【讨论】:
$("#mySelect").val(3);
$("#mySelect option")[2]['selected'] = true; 在调查了 Firebug 中的对象结构后发现了这一点。
$("#mySelect").prop("selectedIndex", 2)
我只是在寻找类似的东西,发现了这个:
$('.mySelect:not(:has(option[selected])) option[value="2"]').attr('selected', true);
这会查找类中尚未选择选项的所有选择菜单,并选择默认选项(在本例中为“2”)。
我尝试使用:selected 而不是[selected],但这不起作用,因为总是选择某些内容,即使没有任何属性具有该属性
【讨论】:
选择框上的更改事件要触发,一旦触发,只需拉取所选选项的 id 属性:-
$("#type").change(function(){
var id = $(this).find("option:selected").attr("id");
switch (id){
case "trade_buy_max":
// do something here
break;
}
});
【讨论】:
这个问题很老了,有很多观点,所以我只是把一些东西扔在那里,我肯定会帮助一些人。
检查一个选择元素是否有任何选定的项目:
if ($('#mySelect option:selected').length > 0) { alert('has a selected item'); }
或者检查一个选择是否没有选择:
if ($('#mySelect option:selected').length == 0) { alert('nothing selected'); }
或者如果您处于某种循环中并且想要检查当前元素是否被选中:
$('#mySelect option').each(function() {
if ($(this).is(':selected')) { .. }
});
检查一个元素是否在循环中未被选中:
$('#mySelect option').each(function() {
if ($(this).not(':selected')) { .. }
});
这些是执行此操作的一些方法。 jQuery 有许多不同的方式来完成同样的事情,所以您通常只需选择看起来最有效的一种。
【讨论】:
if (!$("#select").find("option:selected").length){
//
}
【讨论】:
$("#select_box_id").children()[1].selected
这是在 jquery 中检查选项是否被选中的另一种方法。这将返回布尔值(True 或 False)。
[1] 是选择框选项的索引
【讨论】:
我已经遇到过提到的texotela plugin,这让我可以这样解决:
$(document).ready(function(){
if ( $("#context").selectedValues() == false) {
$("#context").selectOptions("71");
}
});
【讨论】:
这是一个我发现有效的快速脚本... .Result 被分配给一个标签。
$(".Result").html($("option:selected").text());
【讨论】:
$("option[value*='2']").attr('selected', 'selected');
// 2 for example, add * for every option
【讨论】:
<script type="text/javascript">
$(document).ready(function() {
if (!$("#mySelect option:selected").length)
$("#mySelect").val( 3 );
});
</script>
【讨论】:
lencioni's answer 是我推荐的。您可以更改选项('#mySelect option:last') 的选择器以使用“#mySelect option[value='yourDefaultValue']”选择具有特定值的选项。 More on selectors.
如果您在客户端上广泛使用选择列表,请查看此插件: http://www.texotela.co.uk/code/jquery/select/。如果您想查看更多使用选择列表的示例,请查看源代码。
【讨论】:
简单!默认应该是第一个选项。 完成!这会让您使用不显眼的 JavaScript,因为不需要 JavaScript :)
【讨论】:
你们在选择方面做得太多了。只需按值选择:
$("#mySelect").val( 3 );
【讨论】:
这是我更改所选选项的功能。它适用于 jQuery 1.3.2
function selectOption(select_id, option_val) {
$('#'+select_id+' option:selected').removeAttr('selected');
$('#'+select_id+' option[value='+option_val+']').attr('selected','selected');
}
【讨论】:
不需要为此使用jQuery:
var foo = document.getElementById('yourSelect');
if (foo)
{
if (foo.selectedIndex != null)
{
foo.selectedIndex = 0;
}
}
【讨论】:
查看select 元素的selectedIndex。顺便说一句,这是一个普通的 DOM 事情,不是 JQuery 特定的。
【讨论】: