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

时间:2010-10-26 11:32:54

标签: javascript jquery select combobox option

我有两个选择输入,我用几年的数组填充它们。之后我想为每个Select设置currentYear所选的值。我有这个代码,但只适用于第一选择我不明白为什么。

编辑2

jQuery 1.4.3的一切都很好用,1.4.2失败了:_(

修改

我正在尝试这个没有成功:-(只有fisrt选择了值好。可能是版本中的问题?IE6,asp.net 2.0,jquery 1.4.2。例如,“$(sinceComboSelector).val( currentYear);“不起作用我必须做”$('#cmbAnyDesde选项[value ='+ currentYear +']')。attr('selected','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>

由于

1 个答案:

答案 0 :(得分:0)

我在这里测试过,使用上面的代码,然后工作:

$(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);
};