设置DropdownList JQuery的选定值

时间:2016-05-24 12:08:30

标签: jquery

$('.clsComp').change(function () {
                var a = $(this);
                var b = $(this).val();
                $('.clsComp').not(this).each(function () {
                    if ($(this).val() === b)
                    {
                        a.val('-1'); //default value in my datatable
                        alert('Already Exists');
                        return false;
                    }
                });
            });

相关的GridView列

<asp:TemplateField HeaderText="Component">
<ItemTemplate>
    <asp:DropDownList ID="ddlComponent" ClientIDMode="Predictable" runat="server" CssClass="clsComp js-example-placeholder-single" Width="100%"></asp:DropDownList>
    <asp:CompareValidator ID="CompareValidator10" ClientIDMode="Predictable" runat="server" ControlToValidate="ddlComponent" CssClass="error" Display="Dynamic" ErrorMessage="Select" Operator="NotEqual" ValidationGroup="save" ValueToCompare="-1" ForeColor="Red"></asp:CompareValidator>
</ItemTemplate>
<HeaderStyle ForeColor="White" BackColor="#337ab7" Width="14%" HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />

选择现有项目时会发出警报。但是相关选项(given in a.val('-1'))没有被选中(对于任何值,不仅是-1)。帮助赞赏。谢谢!

1 个答案:

答案 0 :(得分:0)

通过此

更改脚本
$('.clsComp').change(function () {
            var a = $(this);
            var b = $(this).val();
            $('.clsComp').not(this).each(function () {
                if ($(this).val() === b)
                {
                    $(this).prop('selected', true);
                    alert('Already Exists');
                    return false;
                }
            });
        });