HTML表单操作和提交问题 [英] HTML form action and onsubmit issues

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

问题描述

我想在某些文本框条目上运行javascript用户验证。



我遇到的问题是我的表单有进入我们网站的新页面的动作,而 onsubmit 属性永远不会运行javascript函数。

是否有更好的解决方案,或者可以与以下代码一起使用的解决方案:注意:如果将操作切换为 checkRegistration()

这只是运行action和javascript时的一个问题。

 < form name =registerFormaction =validate.htmlonsubmit =checkRegistration()method =post > 
<! - 文本框在这里 - >
<! - - 提交按钮 - >
< / form>


解决方案

onsubmit 回调。

 <脚本> 
函数checkRegistration(){
if(!form_valid){
alert('Given data is not correct');
返回false;
}
返回true;
}
< / script>

更新:



这里您有完整的工作示例,表单只会在您输入Google时提交,否则会返回错误:

 < script> 
函数checkRegistration(){
var form_valid =(document.getElementById('some_input')。value =='google');
if(!form_valid){
alert('Given data is incorrect');
返回false;
}
返回true;
}
< / script>
< form onsubmit =return checkRegistration()method =getaction =http://google.com>
写谷歌转到谷歌..< br />
< input type =textid =some_inputvalue =/>
< input type =submitvalue =google it/>
< / form>


I am wanting to run javascript user validation on some textbox entries.

The problem I'm having is that my form has the action of going to a new page within our site, and the onsubmit attribute never runs the javascript function.

Is there a better solution, or one that works with the following code: NOTE: the javascript file is written correctly and works if you switch the action to checkRegistration().

It is merely an issue with running both action and javascript.

<form name="registerForm" action="validate.html" onsubmit="checkRegistration()" method="post">
    <!-- textboxes here -->
    <!-- and submit button -->
</form>

解决方案

You should stop submit procedure by returning false on onsubmit callback.

<script>
function checkRegistration(){
    if(!form_valid){
        alert('Given data is not correct');
        return false;
    }
    return true;
}
</script>
<form onsubmit="return checkRegistration()"...

UPDATE:

here you have fully working example, form will submit only when you write google into input, otherwise it will return error:

<script>
function checkRegistration(){
    var form_valid = (document.getElementById('some_input').value == 'google');
    if(!form_valid){
        alert('Given data is incorrect');
        return false;
    }
    return true;
}
</script>
<form onsubmit="return checkRegistration()" method="get" action="http://google.com">
    Write google to go to google..<br/>
    <input type="text" id="some_input" value=""/>
    <input type="submit" value="google it"/>
</form>

这篇关于HTML表单操作和提交问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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