我有一个验证错误。这个javascript函数根本没有被调用。我的代码:
<form action="." onsubmit="return myform()" name="myform" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Buy">
</form>
<script>
function myform()
{
var nam = document.forms["myform"]["name"];
alert('Checking');
if(some_condition)
{
alert("Sorry. Some Error");
return false;
}
return true;
}
</script>事件,则alert('Checking)不起作用。为甚麽呢?
发布于 2013-12-21 12:17:54
函数名不匹配。这不是buyform()更改为myform()
您需要使用getElementsByName
document.forms["myform"].getElementsByName('name');你能像"myValidateform“一样改变函数名吗?
演示:http://jsfiddle.net/FNBS5/1/
参考:https://developer.mozilla.org/en-US/docs/Web/API/document.getElementsByName
发布于 2013-12-21 12:20:28
试试这个
<form action="javascript:myform()" name="myform" method="post">
<input type="submit" value="Buy"/>
</form>DEMO
https://stackoverflow.com/questions/20715328
复制相似问题