码迷,mamicode.com
首页 > Web开发 > 详细

JS -- 表单

时间:2015-08-09 20:36:52      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

JS – 表单

JS – 表单

var form1 = document.forms.item(0)    # 表示 body 里面的第 1 个表单
document.forms.item(1)                # 表示 body 里面的第 2 个表单

<from …>
<input type="text" name="username" value="some value">
</form>

alert(form1.action);
alert(form1.username); # some value

应用场景, 动态修改表单的提交方式, 如修改 提交到不同的 php 文件

<form action="test.php" method="post" onsubmit="return func()">
<input type="text" name="name" >
<input type="submit" value="submit">
</form>

function func() {
  alert(‘用 js 阻止表单提交‘);
  return false;
}
<form id="form" action="a.php" method="">
...
</form>

function func() {
   form.action = "c.php";
   form.method = "get";
   form.submit();
}
<body onkeydown="return checkNum()">
  <input type="text" id="in">
</body>
<script type="text/javascript">
 function checkNum() {
     var event = window.event || arguments.callee.caller.arguments[0];
     var v = document.getElementById("in").value;
     if (v.length + 1 > 6) {
         alert(‘输入的数字最多为 6‘);
         return false;
     }
     if (event.keyCode >= ‘A‘.charCodeAt(0) && event.keyCode <= ‘Z‘.charCodeAt(0)) {
         alert(‘你输入的不是数字‘);
         return false;
     } else {
         i++;
     }
 }
</script>

JS -- 表单

标签:

原文地址:http://www.cnblogs.com/sunznx/p/4716137.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!