当pdf消息为空时禁用按钮

时间:2015-07-16 14:20:56

标签: javascript c# jquery asp.net-mvc

当文本字段(PDF)为空时,我尝试禁用按钮。我这样试试:

 <div class="col-sm-offset-2 col-lg-6 col-md-8 col-sm-8">
            <div id="hideButtonProduct">
                <br /><br />  <a href="" disabled="@(mailModel.PdfMessage == null || mailModel.PdfMessage == string.Empty)" class="btn btn-success" id="btnGeneratePDF" name="btnGeneratePDF" formtarget="_blank" onclick="generatePdfPreview('GeneratePDFMailProductHandler'); return false;"> &nbsp;&nbsp;&nbsp;<i class="fa fa-fw fa-check"> </i> @Resources.Action.Navigation.PreviewPDF</a>
            </div>
        </div>

但是当没有文字时,按钮仍然可以选择。

谢谢

这是文本框:

 <div class="col-lg-6 col-md-8 col-sm-10 ">
            @Html.EditorFor(model => mailModel.PdfMessage, new { htmlAttributes = new { @class = "form-control tiny-mce", @id = "GeneratePDFMailProductHandler", data_lang = System.Globalization.CultureInfo.CurrentUICulture.Name } })
            @Html.ValidationMessageFor(model => mailModel.PdfMessage)
        </div>

我这样试试:

   <script>

        $(document).ready(function () {
            $("#btnGeneratePDF").prop("disabled", "disabled");

            $("#GeneratePDFMailProductHandler").on("keypress", function () {
                if ($(this).val() != "") {
                    $("#btnGeneratePDF").prop("disabled", false);
                } else {
                    $("#btnGeneratePDF").prop("disabled", "disabled");
                }
            });
        });
    </script>

The problem if I do it like this:

@if (Model.PdfMessage == null || Model.PdfMessage == string.Empty) {


    <script>
        $(document).ready(function () {
            $("#btnGeneratePDFMailFormSubmitter").prop("disabled", "disabled");

            $("#GeneratePDFMailFormSubmitter").on("keypress", function () {
                if ($(this).val() != "") {
                    $("#btnGeneratePDFMailFormSubmitter").prop("disabled", false);
                } else {
                    $("#btnGeneratePDFMailFormSubmitter").prop("disabled", "disabled");
                }
            });
        });


    </script>
}

始终禁用该按钮。即使内有文字

这是我的完整观点:

@model SenecaFormsServer.Areas.Dashboard.Models.EditProductModel
@using SenecaFormsServer.Areas.Dashboard.Models
@using Seneca.SfsLib.Connectors

<div id="ConnectorSettings-MAIL_PRODUCT_HANDLERS">


    <h4>@Resources.Entity.Product.SettingsForConnector: Mail product handlers</h4>

    @{
        MailProductHandlersModel mailModel = Model.ConnectorModels["MAIL_PRODUCT_HANDLERS"] as MailProductHandlersModel;
    }



    <div class="form-group">
        @Html.Label(Resources.Entity.Product.Sender, new { @class = "text-bold control-label col-md-2" })
        <div class="col-lg-6 col-md-8 col-sm-10 ">
            @Html.Editor("MailProductHandlers_sender", new { htmlAttributes = new { @class = "form-control", @Value = mailModel.Sender, @placeholder = Model.CustomerConfig.DefaultSenderAddress } })
        </div>
    </div>

    <div class="form-group">
        @Html.Label(Resources.Entity.Product.MailSubject, new { @class = "text-bold control-label col-md-2" })
        <div class="col-lg-6 col-md-8 col-sm-10 ">
            @Html.Editor("MailProductHandlers_subject", new { htmlAttributes = new { @class = "form-control", @Value = mailModel.Subject } })
        </div>
    </div>

    <!--
    <div class="form-group">
        @Html.Label("Reply to form submitter", new { @class = "text-bold control-label col-md-2" })
        <div class="col-lg-6 col-md-8 col-sm-10 ">
            @Html.CheckBox("MailProductHandlers_replytoFormSubmitter", new { htmlAttributes = new { @class = "form-control" } })
        </div>
    </div>
    -->

    <div class="form-group">
        @Html.Label(Resources.Entity.Product.MailMessage, new { @class = "text-bold control-label col-md-2" })
        <div class="col-lg-6 col-md-8 col-sm-10 ">
            @Html.EditorFor(model => mailModel.Message, new { htmlAttributes = new { @class = "form-control tiny-mce", data_lang = System.Globalization.CultureInfo.CurrentUICulture.Name } })
            @Html.ValidationMessageFor(model => mailModel.Message)
            @*@Html.TextArea("MailProductHandlers_message", mailModel.Message, new { @class = "form-control", @rows = 15 })*@
        </div>
    </div>


    <div class="form-group">
        @Html.Label(Resources.Entity.Product.GeneratePDF, new { @class = "text-bold control-label col-md-2" })
        <div class="col-lg-6 col-md-8 col-sm-10 ">
            @Html.Label(Resources.Entity.Product.GeneratePDFYes) @Html.RadioButtonFor(model => mailModel.IscheckedPDF,true, new {@class = "MAIL_PRODUCT_HANDLERS_AttachPdf" })
            @Html.Label(Resources.Entity.Product.GeneratePDFNo)  @Html.RadioButtonFor(model => mailModel.IscheckedPDF,false, new {@class = "MAIL_PRODUCT_HANDLERS_AttachPdf" })
        </div>
    </div>


    <div class="form-group" id="MAIL_PRODUCT_HANDLERS_pdfeditor">
        <div id="hideLableProduct">
            @Html.Label(Resources.Entity.Product.PdfMessage, new { @id = "hideLable", @class = "text-bold control-label col-md-2" })
        </div>
        <div class="col-lg-6 col-md-8 col-sm-10 ">
            @Html.EditorFor(model => mailModel.PdfMessage, new { htmlAttributes = new { @class = "form-control tiny-mce", @id = "GeneratePDFMailProductHandler", data_lang = System.Globalization.CultureInfo.CurrentUICulture.Name } })
            @Html.ValidationMessageFor(model => mailModel.PdfMessage)
        </div>
        <div class="col-lg-6 col-md-8 col-sm-10 ">
        </div>
        @if (mailModel.PdfMessage == null || mailModel.PdfMessage == string.Empty) {

        }

        <div class="col-sm-offset-2 col-lg-6 col-md-8 col-sm-8">
            <div id="hideButtonProduct">
                <br /><br />  <a href=""  class="btn btn-success" id="btnGeneratePDFMailProductHandler" name="btnGeneratePDF" formtarget="_blank" onclick="generatePdfPreview('GeneratePDFMailProductHandler'); return false;"> &nbsp;&nbsp;&nbsp;<i class="fa fa-fw fa-check"> </i> @Resources.Action.Navigation.PreviewPDF</a>
            </div>
        </div>
    </div>
</div>

@if (!mailModel.IscheckedPDF) {
    <script>
        $(document).ready(function () {
            $('#MAIL_PRODUCT_HANDLERS_pdfeditor').hide();
        });     


    </script>
}

<script>



    $(document).ready(function () {
        $("#btnGeneratePDFMailProductHandler").prop("disabled",true);

        $("#GeneratePDFMailFormSubmitter").on("change",function () {
            if ($(this).val() != '') {
                $("#btnGeneratePDFMailProductHandler").prop("disabled", false);
            } else {
                $("#btnGeneratePDFMailProductHandler").prop("disabled", true);
            }
        });
    });
</script>

奇怪的是,如果我将文本字段留空并保存,那么如果返回到文本字段,则按钮被禁用,但保持禁用状态,即使您将文本放入其中。但如果您在其中放入一些文本并保存并返回,则启用该按钮。

1 个答案:

答案 0 :(得分:0)

试试这个:

$(document).ready(function () {
 $("#btnGeneratePDF").prop("disabled", true);

 $("#GeneratePDFMailProductHandler").on("change", function () {
   if ($(this).val() != "") {
     $("#btnGeneratePDF").prop("disabled", false);
   } else {
     $("#btnGeneratePDF").prop("disabled",true);
       }
     });
});