如何同时设置inputtype=button和inputtype=submit

how do i set input type=button and input type=submit at same time

本文关键字:inputtype submit button 何同时 设置      更新时间:2023-09-26

我有一个html表单,当我按下提交按钮时它会发送-(输入类型=提交)。。同样在同一个按钮上,我有一个onclick事件,它在按下按钮时触发,但由于按钮的类型=提交,我触发的脚本中的window.location不起作用,当我将按钮输入类型更改为按钮时,它可以正常工作,但html表单提交不起作用。。有没有办法两者同时起作用。。请不要通过ajax帖子告诉发布形式。。

这是我的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>

输入不能超过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
    }
}

设置按钮type="button"

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

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

如上所述,如果它确实属于表单元素,那么它的默认行为是"提交"。(它还有一个值属性)

<button>Submit</button>