简体   繁体   English

动态创建提交输入类型

[英]Create a Submit input type dynamically

I am trying to create a submit button dynamically in javascript,but nothing is happening.我正在尝试在 javascript 中动态创建提交按钮,但没有任何反应。 I use MVC and VS2019.我使用 MVC 和 VS2019。

I have -我有 -

    window.addEventListener('load', function () {
        var firstInvalidElement = document.getElementsByClassName("input-validation-error")[0];
        if (firstInvalidElement != undefined) {

            var submitBtn = document.createElement("input");
            submitBtn.type = "submit"
            submitBtn.className = "btn btn - success float - right";
            submitBtn.name = "Submit";
            submitBtn.value = "Submit";
            submitBtn.style.visibility = "visible";

            var parent = document.getElementById("Container");
            parent.appendChild(submitBtn);

            firstInvalidElement.focus();
        }
    });
</script>
 <div class="form-row">
                <div class="col-md-4 mb-3">
                    @Html.LabelFor(model => model.Demographic.Forename, htmlAttributes: new { @class = "control-label" })
                    @Html.EditorFor(model => model.Demographic.Forename, new { htmlAttributes = new { @class = "form-control", maxLength = "100" } })
                    @Html.ValidationMessageFor(model => model.Demographic.Forename, "", new { @class = "text-danger" })
                </div>
                <div class="col-md-4 mb-3">
                    @Html.LabelFor(model => model.Demographic.Surname, htmlAttributes: new { @class = "control-label" })
                    @Html.EditorFor(model => model.Demographic.Surname, new { htmlAttributes = new { @class = "form-control", maxLength = "100" } })
                    @Html.ValidationMessageFor(model => model.Demographic.Surname, "", new { @class = "text-danger" })
                </div>
                <div id="Container"></div>
            </div>

but nothing is shown.但什么也没显示。

Can anyone help please!任何人都可以帮忙吗!

Thanks谢谢

I think it's complicated, try something like this:我认为这很复杂,请尝试以下操作:

       @Html.EditorFor(model => model.Demographic.Surname, new { htmlAttributes = new { @class = " surnameBtn form-control", maxLength = "100" } })


       <button class="btn ToEdit">Edit</button>


<script>
 $("#document").on("click", ".ToEdit", function () {
            var date = new Object();
            date.tDate = $(this).closest('tr').find(".surnameBtn ").val();
</script>

        

I got it working (sort of) -我让它工作(有点)-

I moved code from the Window.addEventListener('load', function () { to -我将代码从 Window.addEventListener('load', function () { 移到 -

 $(function () {
            $('form').submit(function () {
                //debugger;
                if ($(this).find('.input-validation-error').length == 0) {
                    $(this).find(':submit').attr('disabled', 'disabled');
                }
                else {
                    var firstInvalidElement = document.getElementsByClassName("input-validation-error")[0];
                    if (firstInvalidElement != undefined) {

                        var submitBtn = document.createElement("input");
                        submitBtn.type = "submit"
                        submitBtn.className = "btn btn-success float-right";
                        submitBtn.name = "Submit";
                        submitBtn.value = "Submit";
                        submitBtn.style.visibility = "visible";
                        submitBtn.id = "SubmitForm";

                        var parent = document.getElementById("Container");
                        parent.appendChild(submitBtn);

                        firstInvalidElement.focus();
                    }
                }
            });
        });

Thanks guys for your help!谢谢大家帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM