如何使用JS提交表单 [英] How to submit form using JS

查看:105
本文介绍了如何使用JS提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:


我想用JS提交表单,但没有运气。 这里是代码。 JSFIDDLE

 <!DOCTYPE html> 
< html>
< head>
< script type =text / javascriptsrc =js / jquery.js>< / script>
< script type =text / javascript>
函数validate(){
alert('start');
$('#myform')。submit();
alert('end');
}
< / script>
< / head>
< body>
< form id =myformname =myformmethod =postaction =>
< input type =buttonvalue =submitid =submitonclick =validate(); >
< / form>
< / body>
< / html>

问题是为什么它不起作用?

解决方案

您有一个ID为 submit 的表单元素,它与表单方法冲突提交,将提交按钮ID更改为别的。

你可以访问表单元素作为表单对象的属性,所以如果你有一个表单说myForm和一个输入为id或name提交的表单,那么myForm.submit将是id提交的输入元素,而不是原始提交方法。



请参阅 http://jsfiddle.net/4kg8c/1/


Possible Duplicate:
submit is not a function in javascript

I am trying to submit form using JS, but no luck. Here is the code. JSFIDDLE

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript">
    function validate () {
        alert('start');        
        $('#myform').submit();
        alert('end');        
    }
    </script>
</head>
<body>
    <form id="myform" name="myform" method="post" action="">
        <input type="button" value="submit" id="submit" onclick="validate();" >
    </form>
</body>
</html>

The question is "Why its not working?".

解决方案

You have an form element with the id submit which conflicts with the form method submit, change the submit button id to something else.

You can access form elements as properties of the form object, so if you have a form say myForm and an input in the form with id or name submit then myForm.submit will be the input element with id submit and not the original submit method.

See http://jsfiddle.net/4kg8c/1/

这篇关于如何使用JS提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆