澄清暂时禁用提交按钮几秒钟,然后重新启用它?

时间:2013-06-05 09:55:56

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

这与另一个被问到的问题类似,但表格不会提交。

基本上我在这里尝试实现的是,一旦用户点击“提交”btn,btn就被禁用,直到页面被重定向,以避免用户多次点击提交btn。

用户重定向的页面不会显示表单中的信息。 在我介绍禁用和启用功能之前,这确实正常运行。

提供的代码确实禁用了btn,甚至将文本更改为“提交”,但不会重定向到摘要页面,即使我通过选项卡导航到摘要页面,信息也不存在。 非常感谢任何帮助...

表格代码........

  <div class="portlet-body form">
        <!-- BEGIN FORM-->
        @using (Html.BeginForm("RequestAppointment", "Appointment", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", id= "apptReqForm" }))
        {


            @Html.AntiForgeryToken()
            @Html.ValidationSummary(false, string.Empty, new { @class = "alert alert-error" })

            <div class="control-group">
                <label class="control-label">Healthcare Professional</label>
                <div class="controls">         
                    @Html.DropDownListFor(m => m.RequestedUserID, Model.Hcps, string.Empty)

                </div>

            </div>
            <div class="control-group">
                <label class="control-label">@Html.LabelFor(m => m.RequestedDateTime)</label>
                <div class="controls">
                    <div class="input-append" id="ui_date_picker_trigger">
                        @Html.EditorFor(m => m.RequestedDateTime)
                        <span class="add-on"><i class="icon-calendar"></i></span>
                    </div>
                </div>
            </div>

            <div class="control-group">
                <label class="control-label">@Html.LabelFor(m => m.SelectedTime, "Time")</label>
                <div class="controls">
                    <div id="timeSlotID" data-request-url="@Url.Action("LoadTimeslots")">
                        <select id="SelectedTime" name="SelectedTime"></select>
                    </div>
                </div>
            </div>

            <div class="control-group">
                <label class="control-label">@Html.LabelFor(m => m.AdditionalNotes)</label>
                <div class="controls">
                    @Html.TextAreaFor(m => m.AdditionalNotes, new { @class = "span6 m-wrap", rows = 3 })
                    <span class="help-block m-wrap">In the event the selected time you have chosen is not available, please state above if your booking time is flexible.</span>
                </div>
            </div>

            <div class="form-actions">
                <button type="submit" class= "btn blue" id="submit" onclick="disable();"><i class="icon-ok"></i>Submit</button>
                <button type="button" class="btn" onclick="location.href='RequestedAppointments'">Cancel</button>

            </div>
        }
    </div>

   <div class="form-actions">
                <button type="submit" class= "btn blue" id="submit" onclick="disable();"><i class="icon-ok"></i>Submit</button>

  <script>

function disable() {
    x = document.getElementById("submit");
    x.disabled = true;
    x.textContent = "Submitting...";
    document.getElementById("apptReqForm").submit();
    setTimeout(enable, 5000);
}

function enable() {
    x = document.getElementById("submit");
    x.disabled = false;
    x.textContent = " Submit";
}
      </script>          

2 个答案:

答案 0 :(得分:0)

您需要更改脚本以执行以下操作:

function disable() {
    x = document.getElementById("submit");
    x.form.submit();
    x.disabled = true;
    x.textContent = "Submitting...";
    setTimeout(enable, 5000);
}

function enable() {
    x = document.getElementById("submit");
    x.disabled = false;
    x.textContent = " Submit";
}

然而,禁用按钮的事实可能导致回发失败。

尝试查看此解决方案(它使用jQuery) Disable button after clicking once

答案 1 :(得分:0)

你会相信3条简单的线......

将该按钮的ID从“提交”更改为其他任何内容,因为这会混淆Visual Studio

 id="submitreq"

然后将以下内容添加到submitHandler函数..

  var submitButton = document.getElementById("submitreq");
                submitButton.disabled = true;
                submitButton.textContent = "Submitting...";

                form.submit();