简体   繁体   English

<input type=“submit” />不执行javascript代码

[英]<input type=“submit” /> doesn't execute javascript code

Actually, when my form is submitted with the submit button, I would want that the javascript code is executed for checking datas and if datas are not filled, to open a popup dialog and so, don't execute the action bounded to the form. 实际上,使用提交按钮提交表单时,我希望执行javascript代码以检查数据,如果未填充数据,则打开弹出对话框,因此,不要执行与表单绑定的操作。

Unfortunately, the javascript code is never executed even if the javascript is on the top of the page. 不幸的是,即使javascript位于页面顶部,也不会执行javascript代码。

Do you have a solution ? 你有解决方案吗 ?

Thank you 谢谢

My form : 我的表格:

@using (Html.BeginForm("AddRecipe", "AddRecipe", FormMethod.Post, new { data_ajax = "false"}))
{
//Set of components to submit
//etc
<input type="submit" data-theme="b" id="addRecipeBtn" onclick="checkDatas();" value="Ajouter ma recette" />
}

My javascript code to execute : 我要执行的JavaScript代码:

<script>
    function checkDatas() {
            alert("ici");
            var ok = $("#fakeInput").val().length != 0 &&
                 $("#title").val().length != 0 &&
                 $("#cookingTime").val().length != 0 &&
                 $("#preparationTime").val().length != 0 &&
                 $("#ingredientListArea").val().length != 0 &&
                 $("#preparationArea").val().length != 0;

            if (!ok) {
                $("#popupDialog").popup("open");
            }
    }
</script>

We will need more information to determine if or why it's not executing at all. 我们将需要更多信息来确定它是否或为什么根本不执行。

But in order to prevent the form from submitting when validation fails, use event.preventDefault(). 但是为了防止验证失败时提交表单,请使用event.preventDefault()。 This, of course, will prevent the default behavior of the button from executing...in your case, it will prevent form submission. 当然,这将阻止按钮的默认行为执行……就您而言,这将阻止表单提交。

function checkDatas(event) {
        alert("ici");
        var ok = $("#fakeInput").val().length != 0 &&
             $("#title").val().length != 0 &&
             $("#cookingTime").val().length != 0 &&
             $("#preparationTime").val().length != 0 &&
             $("#ingredientListArea").val().length != 0 &&
             $("#preparationArea").val().length != 0;

        if (!ok) {
            event.preventDefault();
            $("#popupDialog").popup("open");
        }
}

I think you will find your form submit is overriding your onclick handler, it would be more reliable in my opinion to catch the form onsubmit event, since the button is of type "submit" 我认为您会发现您的表单提交将覆盖您的onclick处理程序,我认为捕获表单onsubmit事件会更可靠,因为按钮的类型为“ submit”

<form onsubmit="checkDatas()">

removing the onclick event of your button. 删除按钮的onclick事件。

Are you preventing the default submit action of a html form submit? 您是否阻止html表单提交的默认提交动作?

I would think you need something like this: 我认为您需要这样的东西:

function checkDatas(e) {
  e.preventDefault();
  //the rest of your code

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

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