通过传递参数使用JQuery设置html下拉列表的值

时间:2014-02-02 20:10:20

标签: jquery

我有两个下拉列表,我已经从数据库中绑定了数据。

<tr>
            <td>
                <label for="ddlCountry">
                    Country</label>
            </td>
            <td>
                <select id="ddlCountry" name="ddlCountry">
                </select>
            </td>
        </tr>
        <tr>
            <td>
                <label for="ddlCurency">
                    Currency</label>
            </td>
            <td>
                <select id="ddlCurrency" name="ddlCurrency">
                </select>
            </td>
        </tr>

我使用Jquery

将数据绑定到这些数据

对于ddlCurrency:

var $html1 = '';
                $html1 += '<option value="">---Select Currency---</option>';
                if ($currencyList != null || $currencyList != '') {
                    $.each($currencyList, function (index, item) {
                        $html1 += '<option value=' + item.Currency + '>' + item.Currency + '</option>';
                    });
                }
                $('#ddlCurrency').append($html1);

对于ddlCountry:

var $html = '';
                $html += '<option value="">---Select Country---</option>';
                if ($countryList != null || $countryList != '') {
                    var $i = 0;
                    $.each($countryList, function (index, item) {
                        $html += '<option value=' + $i + '>' + item.Country + '</option>';
                        $i++;
                    });
                }
                $('#ddlCountry').append($html);

现在的问题是,当我以编程方式尝试选择下拉列表项时,存在问题。数据绑定没有问题。 当我将参数传递给我的情况来自网格(而不是asp.net gridview)的值时,只有ddlCountry显示带有传递参数的项目,但ddlCurrency显示---选择货币---这是我在ddlCurrency中的第一个项目意思是它没有选择任何东西。

这是通过传递参数来选择下拉项的代码。

$('select[id=ddlCountry] option:contains(' + argus[5] + ')').attr('selected', true);
        $("#ddlCurrency option[value=" + argus[6] + "]").attr('selected', 'selected');

只有第一个选择带有所需项目的下拉列表项目,第二个选择除了两个args都有值

之外的任何内容

现在当我这样做时:

$("#ddlCurrency option[value=" + 'USD' + "]").attr('selected', 'selected');

它选择下拉列表中的项目。

1 个答案:

答案 0 :(得分:0)

argus[6]放在单引号中(Attribute Equals选择器):

$("#ddlCurrency option[value='" + argus[6] + "']")