【问题标题】:how do i set input type=button and input type=submit at same time如何同时设置输入类型=按钮和输入类型=提交
【发布时间】:2014-04-17 15:31:32
【问题描述】:

我有一个 html 表单,当我按下提交按钮时会发送该表单-(input type=submit).. 同样在同一个按钮上,我有一个 onclick 事件,它在按下按钮时触发,但是由于按钮 = 提交的类型,我触发的脚本中的 window.location 不起作用,当我将按钮输入类型更改为时它工作正常按钮,但 html 表单提交不起作用.. 有什么方法可以同时工作..请不要通过ajax post告诉发布表格..

这是我的 html 按钮

<input type="submit" class="submit-login" name="forgotpass" id="" onclick="forgotfunction()"/>

这是我的脚本

<script type="text/javascript">

function forgotfunction(){
    var len = $('#forgetpass').val();
    if (len.length == 10){
        var num = "<?php echo $_SESSION['msg'];unset($_SESSION['msg']);?>"; //getting error from parameter.
        alert(num);
        if(num=="success")
            $('#modalLink').trigger("click");
        if(num=="cannotsend")
            alert("Error! Cannot send mail.,Please try Again!");
        if(num=="invalid")
            alert("Mobile number is  not Registered");
        alert(num);
        alert(window.location.href.split("?")[0]);
        window.location = window.location.href.split("?")[0];//reloading page
        //$('#aforgot').trigger('click');

    }
}
</script>

【问题讨论】:

  • 您的代码刷新页面,不能同时刷新页面和提交表单,即使使用按钮输入,手动提交表单。
  • 提交表单后刷新..为什么会出错??

标签: javascript jquery html ajax forms


【解决方案1】:

您的输入不能超过 1 种类型,即。 submitbutton。要执行您想要的操作,请将类型设置为 button,然后在您的处理逻辑中,手动提交表单。试试这个:

<input type="button" class="submit-login" name="forgotpass" id="" />
$(function() {
    $('.submit-login').click(forgotfunction);
});

function forgotfunction() {
    var len = $('#forgetpass').val();
    if (len.length == 10) {
        var num = "<?php echo $_SESSION['msg']; unset($_SESSION['msg']);?>"; //getting error from parameter.

        if (num == "success")
            $('#modalLink').trigger("click");

        if (num == "cannotsend")
            alert("Error! Cannot send mail.,Please try Again!");

        if (num == "invalid")
            alert("Mobile number is  not Registered");

        window.location = window.location.href.split("?")[0]; //reloading page
    }
}

【讨论】:

    【解决方案2】:

    设置按钮type="button"

    并通过以下方式在按钮单击事件上提交表单: $("#formId").submit()

    【讨论】:

      【解决方案3】:

      我肯定会选择按钮标签..

      here 所述,如果它确实属于表单元素,则默认行为是“提交”。 (它也有一个值属性)

      <button>Submit</button>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-23
        相关资源
        最近更新 更多