0

我已经被困了一段时间,我找不到解决这个问题的方法。

我想启用一个<select>onClick 按钮,但它不起作用:

<div class="col-lg-12">

    <div class="form-group" style="overflow-y: scroll; height: auto; max-height: 545px;">

        <asp:Repeater ID="RepeaterCorrection" runat="server">

            <ItemTemplate>

                <label>
                    <asp:Literal ID="litCorLibelle" runat="server" Text='<%# Eval("DESCRIPTION")%>'></asp:Literal>
                </label>

                <asp:DropDownList ID="ddlCorChamp" runat="server" CssClass="form-control selectpicker"
                    data-live-search="true"></asp:DropDownList>
                <asp:RequiredFieldValidator ID="rfvDropDownList" ForeColor="Red" Display="Dynamic" runat="server"
                    ErrorMessage="Don't forget that field" ControlToValidate="ddlCorChamp"
                    InitialValue="Select something" />

                <p class="help-block"></p>

                <asp:HiddenField ID="hiddenCorIdChamp" runat="server" Value='<%# Eval("IDCHAMP") %>' />
                <asp:HiddenField ID="hiddenCorNameChamp" runat="server" Value='<%# Eval("NAMECHAMP")%>' />
                <asp:HiddenField ID="hiddenCorType" runat="server" Value='<%# Eval("TYPE")%>' />
            </ItemTemplate>

        </asp:Repeater>

    </div>

    <asp:Button ID="btnValid" runat="server" Text="Let's Enable this !"
        class="btn btn-primary btn-block" CausesValidation="False" UseSubmitBehavior="False" Enabled="True" />
</div>
//IT WORKS, IT IS DISABLED
$('Select[Champ=ID_REJECT]').prop('disabled', true);

//IT DOESN'T WORK, IT IS STILL DISABLED, EVEN WHEN I CLICK
$('#<%= btnValid.ClientID %>').click(function () {
    $('Select[Champ=ID_REJECT]').prop('disabled', false);
    //$('Select[Champ=ID_REJET]').removeAttr("disabled");
    //$('Select[Champ=ID_REJECT]').attr('disabled', false);
    return false;
});

我真的不明白为什么......你知道吗?

4

1 回答 1

0

这其中的几个原因可能有几个原因:

原因 1
您在jquery事件绑定中对按钮的 id 进行了硬编码,如果您的 DOM 正在被修改,例如当您在ContentPlaceHolder. 您需要做的是,像这样修改您的按钮事件接线:

$('#<%= btnValid.ClientID %>').click(function () {
    alert("Hey");
});

原因 2
服务器端按钮将始终默认执行,您可能希望通过在事件结束时postback添加语句来覆盖此行为。return false;像这样:

$('#<%= btnValid.ClientID %>').click(function () {
    alert("Hey");
    return false; // This will tell the event that we do not want to proceed further with posting the data to server.
});

如果这两种解决方案中的任何一种对您有帮助,请告诉我。

更新

因为,您想启用/禁用您的jquery select元素,我建议使用prop()方法来实现所需。

$('#<%= btnValid.ClientID %>').click(function () {
    $('Select[Champ=ID_REJECT]').prop("disabled", false);
    return false;
});

或者您可以从 select 元素中删除 disabled 属性,如下所示:

$('#<%= btnValid.ClientID %>').click(function () {
    $('Select[Champ=ID_REJECT]').removeAttr("disabled"); // this will entirely remove the attribute
    return false;
});
于 2020-10-29T12:11:56.497 回答