我有一个已知值的下拉列表。我要做的是将下拉列表设置为我知道使用jQuery存在的特定值。 使用常规的JavaScript,我会做如下的事情:

ddl = document.getElementById("ID of element goes here");
ddl.value = 2; // 2 being the value I want to set it too.

然而,我需要用jQuery做到这一点,因为我使用CSS类为我的选择器(愚蠢的ASP。NET客户端id…)。

以下是我尝试过的一些方法:

$("._statusDDL").val(2); // Doesn't find 2 as a value.
$("._statusDDL").children("option").val(2) // Also failed.

我如何用jQuery做到这一点?


更新

所以事实证明,我第一次就说对了:

$("._statusDDL").val(2);

当我把警报放在上面时,它工作得很好,但当我删除警报并让它全速运行时,我得到了错误

无法设置所选属性。无效的指数

我不确定这是jQuery或Internet Explorer 6的bug(我猜是Internet Explorer 6),但这非常烦人。


当前回答

这些解决方案似乎假设下拉列表中的每个项都有一个val()值,该值与它们在下拉列表中的位置相关。

如果不是这样,事情就复杂了。

要读取下拉列表的选定索引,您可以使用以下命令:

$("#dropDownList").prop("selectedIndex");

要设置下拉列表的选定索引,您可以使用以下命令:

$("#dropDownList").prop("selectedIndex", 1);

注意prop()特性需要JQuery v1.6或更高版本。

让我们看看如何使用这两个函数。

假设您有一个月份名称的下拉列表。

<select id="listOfMonths">
  <option id="JAN">January</option>
  <option id="FEB">February</option>
  <option id="MAR">March</option>
</select>

你可以添加一个“前一个月”和“下一个月”按钮,它会查看当前选择的下拉列表项,并将其更改为前一个月/下个月:

<button id="btnPrevMonth" title="Prev" onclick="btnPrevMonth_Click();return false;" />
<button id="btnNextMonth" title="Next" onclick="btnNextMonth_Click();return false;" />

下面是这些按钮运行的JavaScript代码:

function btnPrevMonth_Click() {
    var selectedIndex = $("#listOfMonths").prop("selectedIndex");
    if (selectedIndex > 0) {
        $("#listOfMonths").prop("selectedIndex", selectedIndex - 1);
    }
}
function btnNextMonth_Click() {
    //  Note:  the JQuery "prop" function requires JQuery v1.6 or later
    var selectedIndex = $("#listOfMonths").prop("selectedIndex");
    var itemsInDropDownList = $("#listOfMonths option").length;

    //  If we're not already selecting the last item in the drop down list, then increment the SelectedIndex
    if (selectedIndex < (itemsInDropDownList - 1)) {
        $("#listOfMonths").prop("selectedIndex", selectedIndex + 1);
    }
}

我的网站也有助于展示如何使用JSON数据填充下拉列表:

http://mikesknowledgebase.com/pages/Services/WebServices-Page8.htm

其他回答

试试

$("._statusDDL").val("2");

而不是和

$("._statusDDL").val(2);

所以我把它改了 在300毫秒使用后执行 setTimeout。现在好像起作用了。

在从Ajax调用加载数据时,我遇到过很多次这种情况。我也使用。net,当使用jQuery选择器时,它需要时间来调整到clientId。为了纠正您遇到的问题并避免必须添加setTimeout属性,您可以简单地在Ajax调用中放置“async: false”,它将给DOM足够的时间来返回您添加到select中的对象。下面是一个小样本:

$.ajax({
    type: "POST",
    url: document.URL + '/PageList',
    data: "{}",
    async: false,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
        var pages = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;

        $('#locPage' + locId).find('option').remove();

        $.each(pages, function () {
            $('#locPage' + locId).append(
                $('<option></option>').val(this.PageId).html(this.Name)
            );
        });
    }
});

仅供参考,你不需要使用CSS类来完成这一点。

你可以写下面这行代码在客户端上获得正确的控件名称:

$("#<%= statusDDL.ClientID %>").val("2");

ASP。NET将在jQuery内部正确地呈现控件ID。

jQuery的文档声明:

(jQuery。Val]检查或选择所有单选按钮、复选框,并选择与该值集匹配的选项。

这个行为在jQuery 1.2及以上版本中。

你很可能想要这个:

$("._statusDDL").val('2');

添加.change()以查看下拉列表前端的选项:

$("._statusDDL").val('2').change();

另一种选择是在。net中设置控件参数ClientID="Static",然后你就可以通过你设置的ID在JQuery中访问对象。