Javascript 检查是否使用jQuery选择了选项,如果未选择,则选择默认值

Javascript 检查是否使用jQuery选择了选项,如果未选择,则选择默认值,javascript,jquery,forms,dom,html-select,Javascript,Jquery,Forms,Dom,Html Select,使用jQuery,如何检查选择菜单中是否有选中的选项,如果没有,则将其中一个选项指定为选中的选项 (select是在我刚刚继承的一个应用程序中通过一系列PHP函数生成的,因此,这是一个快速修复方法,同时我还要考虑这些:)看看select元素的用法。顺便说一句,这是一个普通的ol'DOM东西,不是特定于JQuery的。无需使用JQuery: var foo = document.getElementById('yourSelect'); if (foo) { if (foo.selected

使用jQuery,如何检查选择菜单中是否有选中的选项,如果没有,则将其中一个选项指定为选中的选项


(select是在我刚刚继承的一个应用程序中通过一系列PHP函数生成的,因此,这是一个快速修复方法,同时我还要考虑这些:)

看看
select
元素的用法。顺便说一句,这是一个普通的ol'DOM东西,不是特定于JQuery的。

无需使用JQuery:

var foo = document.getElementById('yourSelect');
if (foo)
{
   if (foo.selectedIndex != null)
   {
       foo.selectedIndex = 0;
   } 
}

虽然我不确定您到底想要完成什么,但这段代码对我很有用

<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>

弗斯特
第二
第三
第四
$(文档).ready(函数(){
如果(!$(“#我的选择选项:选中”).length){
$(“#mySelect option[value='3']”)attr('selected','selected');
}
});
是我的建议。您可以更改选项
(“#mySelect option:last”)
的选择器,以使用“
#mySelect option[value='yourlaultvalue']
”选择具有特定值的选项

如果您在客户端上广泛使用选择列表,请查看此插件: . 如果您想看到更多使用选择列表的示例,请查看源代码。

我已经遇到了上面提到的问题,我可以这样解决:

$(document).ready(function(){
    if ( $("#context").selectedValues() == false) {
    $("#context").selectOptions("71");
    }
});

这是我更改所选选项的函数。它适用于jQuery1.3.2

function selectOption(select_id, option_val) {
    $('#'+select_id+' option:selected').removeAttr('selected');
    $('#'+select_id+' option[value='+option_val+']').attr('selected','selected');   
}

这是一个我发现有效的快速脚本。。。 。结果将指定给标签

$(".Result").html($("option:selected").text());

你们做的太多了,无法选择。只需按值选择:

$("#mySelect").val( 3 );

轻松默认值应为第一个选项完成这将导致您使用不引人注目的JavaScript,因为不需要JavaScript:)


$(文档).ready(函数(){
如果(!$(“#我的选择选项:选中”).length)
$(“#我的选择”).val(3);
});
这是在jquery中检查选项是否被选中的另一种方法。这将返回布尔值(True或False)


[1] 是选择框选项的索引

这个问题很老,而且有很多视图,所以我会把一些东西扔出去,我相信这会帮助一些人

if (!$("#select").find("option:selected").length){
  //
}
要检查选择元素是否有任何选定项,请执行以下操作:

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')) { .. }
});
或者,如果您处于某种循环中,并且希望检查当前元素是否被选中:

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有许多不同的方法来完成同一件事,因此您通常只需选择其中一种最有效的方法。

选择框上的Change事件将被触发,一旦触发,则只需拉动所选选项的id属性:-

$("#type").change(function(){
  var id = $(this).find("option:selected").attr("id");

  switch (id){
    case "trade_buy_max":
      // do something here
      break;
  }
});

我只是在寻找类似的东西,发现:

$('.mySelect:not(:has(option[selected])) option[value="2"]').attr('selected', true);
这将查找类中尚未选择选项的所有选择菜单,并选择默认选项(在本例中为“2”)


我尝试使用
:selected
而不是
[selected]
,但这不起作用,因为始终选择了某些内容,即使没有任何内容具有该属性

我找到了一个很好的方法来检查选项是否已选中,并在未选中时选择默认值

    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>');
    }
if(!$('some#u select option[selected=“selected”]').val(){
//如果未选择值,则在此处输入代码
//例如,添加第一个值作为“占位符”
$('some#u select option:first child')。在('Wybierz:'之前);
}

如果#some_select没有默认选择选项,则.val()是未定义的

如果需要显式检查每个选项以查看是否有任何选项具有“selected”属性,则可以执行此操作。否则,使用
选项:selected
将获得第一个默认选项的值

var viewport_selected = false;      
$('#viewport option').each(function() {
    if ($(this).attr("selected") == "selected") {
        viewport_selected = true;
    }
});

OP说,“使用jQuery…”所以忽略这一点似乎是多余的,不是吗?就我个人而言,如果我问一个特定的问题,我希望得到问题的答案,而不是另一种方式。有时你寻求的答案不是你期望的答案。不管怎样,他问的是jQuery,jQuery的答案更容易理解,特别是当一个人不理解基本JavaScript时。这是一个很好的方法,有时候基本JavaScript非常方便!这是选择选项3,不管是否已经选择了另一个选项。该选项将更易于阅读,如下所示:
$(“#mySelect”).val(3)
这让我走上了正确的道路,但我需要这样做:
$(“#mySelect option”)[2]['selected']=true发现了这一点。从jQuery1.6开始,您可以直接设置属性
$(“#mySelect”).prop(“selectedIndex”,2)
.val(x)版本在Internet Explorer 8中不起作用,因此@gradbot是正确的,因为它在所有浏览器中都起作用。“not”对我不起作用$(“#mySelect option”).each(函数(){if($(this).not(':selected'){..});因此,在上使用否定类似于if(!$(this).is(':selected'){..}Browser:Chrome;Jquery版本:3.1.1
var viewport_selected = false;      
$('#viewport option').each(function() {
    if ($(this).attr("selected") == "selected") {
        viewport_selected = true;
    }
});