如何使用 jQuery 将选定值设置为多个 Select 输入?

发布于 2024-09-28 18:15:03 字数 2787 浏览 2 评论 0原文

我有 2 个选择输入,并用一系列年份填充它们。之后,我想为每个 Select 设置 currentYear 的选定值。我有这段代码,但仅适用于第一个选择我不明白为什么。

jQuery 1.4.3 一切正常,1.4.2 失败:_(

我正在尝试此操作,但没有成功:-( 只有 fisrt 选择了值 OK。可能是版本中的问题?IE6、asp.net 2.0、jquery 1.4。 2. 例如,“$(sinceComboSelector).val(currentYear);”不起作用,我必须执行“$('#cmbAnyDesde option[value=' + currentYear + ']').attr('selected' , '选定');" :-(

代码更新:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Tests.aspx.cs" Inherits="Tests" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="../JAVASCRIPT/jquery-1.4.2.min.js" type="text/javascript"></script>
    </head>
    <body>
        <script type="text/javascript">
            $(document).ready(function() {
                loadYears();
            });
            var loadYears = function() {
                var currentYear = new Date().getFullYear(),
                    years = [],
                    firstYear = currentYear - 7,
                    lastYear = currentYear + 3,
                    sinceComboSelector = "#cmbAnyDesde",
                    toComboSelector = "#cmbAnyHasta",
                    i;
    
                for (i = firstYear; i <= lastYear; i++) {
                    years.push(i);
                }
    
                $(sinceComboSelector).find('option').remove();
    
                $(toComboSelector).find('option').remove();
    
                $.each(years, function(key, value) {
                    $(sinceComboSelector)
                      .append($("<option></option>")
                      .attr("value", value)
                      .text(value));
    
                    $(toComboSelector)
                      .append($("<option></option>")
                      .attr("value", value)
                      .text(value)); ;
                });
    
    //            $(sinceComboSelector).val(currentYear);
    //            $(toComboSelector).val(currentYear);
                $('#cmbAnyDesde option[value=' + currentYear + ']').attr('selected', 'selected');            
                $('#cmbAnyHasta option[value=' + currentYear + ']').attr('selected', 'selected');
            }; 
        </script>
        <form id="form1" runat="server">
        <div>
            <select id="cmbAnyDesde" style="width:70px"></select>
            <select id="cmbAnyHasta" style="width:70px"></select>
        </div>
        </form>
    </body>
</html>

I have 2 select inputs and I filling them with an array of years. After that I want to set currentYear the selected value for every Select. I hacve this code but only works with first Select I don't understand why.

Everything works great with jQuery 1.4.3, 1.4.2 fails :_(

I'm trying this without success :-( only fisrt has selected value OK. Can be a problem in versions? IE6, asp.net 2.0, jquery 1.4.2. For example, "$(sinceComboSelector).val(currentYear);" doesn't work i have to do a "$('#cmbAnyDesde option[value=' + currentYear + ']').attr('selected', 'selected');" :-(

Code updated:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Tests.aspx.cs" Inherits="Tests" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="../JAVASCRIPT/jquery-1.4.2.min.js" type="text/javascript"></script>
    </head>
    <body>
        <script type="text/javascript">
            $(document).ready(function() {
                loadYears();
            });
            var loadYears = function() {
                var currentYear = new Date().getFullYear(),
                    years = [],
                    firstYear = currentYear - 7,
                    lastYear = currentYear + 3,
                    sinceComboSelector = "#cmbAnyDesde",
                    toComboSelector = "#cmbAnyHasta",
                    i;
    
                for (i = firstYear; i <= lastYear; i++) {
                    years.push(i);
                }
    
                $(sinceComboSelector).find('option').remove();
    
                $(toComboSelector).find('option').remove();
    
                $.each(years, function(key, value) {
                    $(sinceComboSelector)
                      .append($("<option></option>")
                      .attr("value", value)
                      .text(value));
    
                    $(toComboSelector)
                      .append($("<option></option>")
                      .attr("value", value)
                      .text(value)); ;
                });
    
    //            $(sinceComboSelector).val(currentYear);
    //            $(toComboSelector).val(currentYear);
                $('#cmbAnyDesde option[value=' + currentYear + ']').attr('selected', 'selected');            
                $('#cmbAnyHasta option[value=' + currentYear + ']').attr('selected', 'selected');
            }; 
        </script>
        <form id="form1" runat="server">
        <div>
            <select id="cmbAnyDesde" style="width:70px"></select>
            <select id="cmbAnyHasta" style="width:70px"></select>
        </div>
        </form>
    </body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离旧人 2024-10-05 18:15:03

我在这里用上面的代码进行了测试,并且有效:

$(document).ready(function (){

  loadYears();

});

var loadYears = function () {
  var currentYear = new Date().getFullYear(),
    years = [],
    firstYear = currentYear - 7,
    lastYear = currentYear + 3,
    sinceComboSelector = "#cmbAnyDesde",
    toComboSelector = "#cmbAnyHasta",
    i;

  for (i = firstYear; i <= lastYear; i++){
    years.push(i);
  }

  $(sinceComboSelector).find('option').remove();

  $(toComboSelector).find('option').remove();

  $.each(years, function(key, value) {
    $(sinceComboSelector)
      .append($("<option></option>")
      .attr("value", value)
      .text(value));

    $(toComboSelector)
      .append($("<option></option>")
      .attr("value", value)
      .text(value)); ;
  });

  $(sinceComboSelector).val(currentYear);
  $(toComboSelector).val(currentYear);
};

I tested here, with this code above, and works:

$(document).ready(function (){

  loadYears();

});

var loadYears = function () {
  var currentYear = new Date().getFullYear(),
    years = [],
    firstYear = currentYear - 7,
    lastYear = currentYear + 3,
    sinceComboSelector = "#cmbAnyDesde",
    toComboSelector = "#cmbAnyHasta",
    i;

  for (i = firstYear; i <= lastYear; i++){
    years.push(i);
  }

  $(sinceComboSelector).find('option').remove();

  $(toComboSelector).find('option').remove();

  $.each(years, function(key, value) {
    $(sinceComboSelector)
      .append($("<option></option>")
      .attr("value", value)
      .text(value));

    $(toComboSelector)
      .append($("<option></option>")
      .attr("value", value)
      .text(value)); ;
  });

  $(sinceComboSelector).val(currentYear);
  $(toComboSelector).val(currentYear);
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文