简体   繁体   English

我需要禁用提交按钮,直到所有字段都具有值

[英]I need to disable submit button until all the fields are having values

I want to disable submit button until all the fields are having values. 我想禁用提交按钮,直到所有字段都具有值。 I have seen few examples having tags. 我见过一些带有标签的例子。 But here I am using @Html.TextBoxFor. 但是这里我使用@ Html.TextBoxFor。 I have tried out something after referring to few examples online. 我在网上参考了几个示例之后就尝试了一些方法。 But I am still unable to achieve the expected result. 但是我仍然无法达到预期的结果。 Any help please ? 有什么帮助吗?

Here is my html code: 这是我的html代码:

   @using (Html.BeginForm("User", "Plan"))
            {
                <div class="row">
                    <div class="col-lg-8">
                        <p id="myElem" style="display:none; font-style:italic;background-color:yellow">Floor Plan Saved Successfully</p>
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label class="control-label col-lg-4">Period:</label>
                                <div class="col-lg-8">
                                    @Html.DropDownList("Quarter", new SelectListItem[] { (new SelectListItem() { Text = "Q1", Value = "1" }), (new SelectListItem() { Text = "Q2", Value = "2" }), (new SelectListItem() { Text = "Q3", Value = "3" }), (new SelectListItem() { Text = "Q4", Value = "4" }) }, "-- Select Quarter --", new { @class = "form-control" })
                                    <br />
                                    @Html.DropDownList("Year", new SelectListItem[] { (new SelectListItem() { Text = "2016", Value = "2016" }), (new SelectListItem() { Text = "2017", Value = "2017" }) }, "-- Select Year --", new { @class = "form-control" })
                                </div>
                            </div>
                        </div>
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label class="control-label col-lg-4">Line ID:</label>
                                <div class="col-lg-8">
                                    @Html.TextBoxFor(model => model.floorConfig.LineID, new { onkeypress = "return isNumberKey(event)", @class = "form-control" })
                                </div>
                            </div>
                        </div>



                <div class="row">
                    <div class="col-lg-offset-4" style="padding: 10px 0px 0px 0px;">
                        <input type="submit" id="submit" value="Submit" class="btn btn-lg btn-success" />
                        <input type="button" id="btnCancel" value="Clear" class="btn btn-lg btn-success" />
                    </div>
                </div>

            }

Below is the script i have written to disable the submit button. 以下是我编写的禁用提交按钮的脚本。

<script type="text/javascript">
    $(document).ready(function () {

    $("#Quarter, #Year, #LineID").keyup(function () {
        $(".submit").attr('disabled', $(this).val() && $(this).val() != "-- Select Quarter --" && $(this).val() != "-- Select Year --" ? "" : 'disabled');
    });


});
</script>
$("#Quarter, #Year, #LineID").keyup(function () {
    $(".submit").prop("disabled", !$("#Quarter").val() || !$("#Year").val() || !$("#LineID").val());
});

$("#someId").val() is false if its value is null, so it will make the value of the attribute "disabled" to true. $(“#someId”)。val()如果值为null,则为false,因此会将属性“ disabled”的值设为true。

In case you don't know, 万一你不知道

$(this).val() ? "" : "disable")

is an internal if/else. 是内部if / else。 It basically means the same as 它的基本含义与

if( $(this).val()){ // if this has a value not null or empty
//"disable" attribute takes "" as value
}else{
//"disable" attribute takes "disable" as value
}

I hope it will work. 我希望它能起作用。 If not, it means that JS considers your dropdownlist basic selection as a value, so you have more checks to do between $(this).val() and the question mark, like 如果不是,则表示JS将下拉列表的基本选择视为一个值,因此您需要在$(this).val()和问号之间进行更多检查,例如

$("#Quarter, #Year, #LineID").keyup(function () {
    $(".submit").prop("disabled", !$("#Quarter").val() || !$("#Year").val() || !$("#LineID").val() || $("#Quarter").val() == "-- Select Quarter --" || $("#Year").val() == "-- Select Year --");
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 禁用提交按钮,直到完成所有字段 - Disable submit button until all the fields are complete 禁用jQuery的提交按钮,直到所有字段都具有值(输入,单选,选择,复选框) - Disable submit button with jQuery until all fields have values (input, radio, select, checkbox) Javascript:如何在所有字段都经过验证之前禁用提交按钮? - Javascript: How to disable submit button until all fields are validated? 禁用提交按钮,直到验证所有表单字段 - Disable Submit button until all form fields are validated 如何在引导模式中完成所有字段之前禁用提交按钮? - How do I disable the submit button until all fields are complete in a bootstrap modal? 禁用提交按钮,直到填写输入字段 - Disable submit button until input fields are filled 禁用提交按钮,直到填写字段 - Disable Submit button until fields are filled in Javascript - 禁用提交按钮,直到3个字段不为空 - Javascript - Disable submit button until 3 fields are not empty 在 ReactJS 中填写所有输入字段之前,如何禁用表单提交按钮? - How to disable form submit button until all input fields are filled in ReactJS? 如何禁用提交按钮,直到所有文本字段都已填满并且选择了文件 - How to disable submit button until all text fields are full and file is selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM