用javascript 实现
有7个文本框,回车的时候阻止表单提交,无论文本框为空或不为空光标都转移到下一个文本框,每一次回车光标都往后移一个文本框,直到最后一个文本框,回车就提交,如何实现呢?以下有一段代码供参考,该如何修改才能实现以上功能呢?
function killEnter(e){
if (document.form1.danjia.value=="" ||document.form1.jian.value=="")
{
e=e||window.event;
if(e.keyCode==13){//回车则阻止默认提交表单的事件
if (document.form1.danjia.value=="")
{
document.all.danjia.focus();
}
if (document.form1.jian.value=="")
{
document.all.jian.focus();
}
if (document.form1.danwei.value=="")
{
document.all.danwei.focus();
}
if (document.form1.guige.value=="")
{
document.all.guige.focus();
}
if (document.form1.chanpinname.value=="")
{
document.all.chanpinname.focus();
}
if (document.form1.yuangong.value=="")
{
document.all.yuangong.focus();
}
if (document.form1.shengchandate.value=="")
{
document.all.shengchandate.focus();
}
if(e.preventDefault)e.preventDefault();//FF
else e.returnValue=false;
}
return false;
}
}
有7个文本框,回车的时候阻止表单提交,无论文本框为空或不为空光标都转移到下一个文本框,每一次回车光标都往后移一个文本框,直到最后一个文本框,回车就提交,如何实现呢?以下有一段代码供参考,该如何修改才能实现以上功能呢?
function killEnter(e){
if (document.form1.danjia.value=="" ||document.form1.jian.value=="")
{
e=e||window.event;
if(e.keyCode==13){//回车则阻止默认提交表单的事件
if (document.form1.danjia.value=="")
{
document.all.danjia.focus();
}
if (document.form1.jian.value=="")
{
document.all.jian.focus();
}
if (document.form1.danwei.value=="")
{
document.all.danwei.focus();
}
if (document.form1.guige.value=="")
{
document.all.guige.focus();
}
if (document.form1.chanpinname.value=="")
{
document.all.chanpinname.focus();
}
if (document.form1.yuangong.value=="")
{
document.all.yuangong.focus();
}
if (document.form1.shengchandate.value=="")
{
document.all.shengchandate.focus();
}
if(e.preventDefault)e.preventDefault();//FF
else e.returnValue=false;
}
return false;
}
}
解决方案 »
- 日期控件jsp里好用。在js里写的不好用。跪求解决办法。大神们!!!
- 要这个函数有什么用?知道的达人帮我看下!
- 如何实现鼠标移到图片的左半部分显示“上一张”,右半部分显示“下一张”,点击进入下一张或上一张图片
- js的传值问题
- 请问这个正则表达式该怎么写?
- 关于新窗口问题
- ★★★请问各位老师如何在页面中使用脚本得到该页面所有的CheckBox集合★★★
- 请教关于内容提交后网页重定向的问题~~~~~~~事关五一放假!谢谢
- js代码实现获取鼠标悬停在某个按钮上时时间
- 百度地图API for JS中点的信息窗口中添加html控件text文本框和按钮,如何给text文本框的value赋默认值
- 已定义数组如何判断使用哪一个
- 关于动态改变innerHTML问题
<form id="form1">
<input id="inp1" onkeyup="func1(this);"/><br/>
<input id="inp2" onkeyup="func1(this);"/><br/>
<input id="inp3" onkeyup="func1(this);"/><br/>
<input id="inp4" onkeyup="func1(this);"/><br/>
<input id="inp5" onkeyup="func1(this);"/><br/>
<input id="inp6" onkeyup="func1(this);"/><br/>
<input id="inp7" onkeyup="func1(this);"/><br/>
</form>
<script>
function func1(obj){
var e=window.event;
if(e.keyCode==13){
var id=obj.id;
var count=Number(id.replace("inp",""));
if(count==7){
alert("提交了");
}else{
document.getElementById("inp"+(count+1)).focus();
}
}
}
</script>
<form id="form1" action="123.asp">
<input id="inp1" onkeyup="func1(this);"/><br/>
<input id="inp2" onkeyup="func1(this);"/><br/>
<input id="inp3" onkeyup="func1(this);"/><br/>
<input id="inp4" onkeyup="func1(this);"/><br/>
<input id="inp5" onkeyup="func1(this);"/><br/>
<input id="inp6" onkeyup="func1(this);"/><br/>
<input id="inp7" onkeyup="func1(this);"/><br/>
<label>
<input type="submit" name="Submit" value="提交" />
</label>
</form>
<script>
function func1(obj){
var e=window.event;
if(e.keyCode==13){
var id=obj.id;
var count=Number(id.replace("inp",""));
if(count==7){
alert("提交了");
}else{
document.getElementById("inp"+(count+1)).focus();
}
}
}
</script>可是有两个问题
第一 回车以后 提交了
第二 你改了 我的文本 id 这样我的整套系统 相应的ID 都得修改,工程不小呀.
form1.submit();
}else{不是只有7个文本框么
<body>
<form action='http://www.baidu.com' name='myForm'>
<input />
<input />
<input />
<input />
<input />
<input />
<input name='lastInput'/>
</form></body>
<script type="text/javascript">
window.onload=function(){ document.body.onkeydown=function(e){
e= e|| event;
var obj = e.target||e.srcElement;
if(e.keyCode == 13 )
{
if(obj.name=="lastInput"){
document.forms["myForm"].submit();
}else
{
if(!document.all){
getNextChild( obj).focus();
}else{
e.keyCode = 9;
}
}
}
};
};//获取下一个有焦点的节点
function getNextChild(obj) {
var result = obj.nextSibling;
while (!result.tagName) {
result = result.nextSibling;
}
return result;
}
</script></html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>让你的回车转为TABLE
</title>
</head>
<body>
<form id="form1">
<input type="text" name="a1" onkeyup="cgo(this)"><!-- 将回车转换为Tab -->
<input type="text" name="a2" onkeyup="cgo(this)">
<input type="text" name="a3" onkeyup="cgo(this)">
<input type="text" name="a4" onkeyup="cgo(this)">
<input type="text" name="a5" onkeyup="cgo(this)">
<input type="text" name="a6" onkeyup="cgo(this)">
<input type="text" name="a7" onkeyup="cgo(this)">
</form>
<script language="javascript">
var obj = document.getElementById("form1").getElementsByTagName("input");
function cgo(element){
if(event.keyCode==13){for(var tmpa=0;tmpa<obj.length;tmpa++){
if(obj[tmpa].name==element.name){
var id=tmpa;
}
}
if(id<obj.length-1){
obj[id+1].focus();
}
else {
element.form.submit();
}
}
}
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function func1(e,ob){
var form=document.getElementsByTagName('form')[0],next=ob.nextSibling;
e=e||window.event;
if(e.keyCode==13){
while(next){
if(next.nodeName=='INPUT' && next.type=='text'){
next.focus();
return false;
}
next=next.nextSibling;
}
form.submit();
}
}
</script>
</head><body>
<form action='http://www.baidu.com'>
<input onkeyup="func1(event,this);" type="text" /><br/>
<input onkeyup="func1(event,this);" type="text" /><br/>
<input onkeyup="func1(event,this);" type="text" /><br/>
<input onkeyup="func1(event,this);" type="text" /><br/>
<input onkeyup="func1(event,this);" type="text" /><br/>
<input onkeyup="func1(event,this);" type="text" /><br/>
<input onkeyup="func1(event,this);" type="text" /><br/>
</form>
</body>
</html>以上代码经过测试,兼容IE OPERA chrome firefox
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<body>
<form action="" method="post">
<table id="d1">
<tr>
<td>
<input type="text" id="tx1" name="tx1" onkeyup="killEnter(event)">
</td>
</tr>
<tr>
<td>
<input type="text" id="tx2" name="tx2" onkeyup="killEnter(event)">
</td>
</tr>
<tr>
<td>
<input type="text" id="tx3" name="tx3" onkeyup="killEnter(event)">
</td>
</tr>
<tr>
<td>
<input type="text" id="tx4" name="tx4" onkeyup="killEnter(event)">
</td>
</tr>
<tr>
<td>
<input type="text" id="tx5" name="tx5" onkeyup="killEnter(event)">
</td>
</tr>
<tr>
<td>
<input type="text" id="tx6" name="tx6" onkeyup="killEnter(event)">
</td>
</tr>
<tr>
<td>
<input type="text" id="tx7" name="tx7" onkeyup="killEnter(event)">
</td>
</tr>
</table>
</form> </body>
<script type="text/javascript">
function killEnter(e)
{
e = e || window.event;
if (e.keyCode == 13)
{
var doc = document;
var arr = doc.getElementById('d1').getElementsByTagName('input');
var srcElement = e.srcElement || e.target;
var srcId = srcElement.id;
if (srcId != 'tx7')
{
srcId = Number(srcId.replace("tx", ""));
srcId++;
doc.getElementById('tx' + srcId).focus();
}
else
{
doc.forms[0].submit();
}
}
}
</script>
</html>
<head>
<title></title>
<script type="text/javascript">
/**
* 文本框回车事件
* @param {} e
* 事件对象 event
* @param {} ob
* 触发事件的文本框对象
*/
function func1(e,ob){
var form=document.getElementsByTagName('form')[0],texts=form.getElementsByTagName('input');
for(var i=0,il=texts.length;i<il;i++){
var text=texts.item(i);
if(ob!=text && i<il-1) continue;
if(ob==text && i<il-1){
texts.item(i+1).focus();
return false;
}
if(i==il-1)form.submit();
}
}
/**
* 页面加载事件,在这里为需要设置的form添加文本框事件
*/
function addTextEvent(){
var form=document.getElementsByTagName('form')[0],childs=form.getElementsByTagName('input');//form根据自己的需要设置
var tmpEvent=function(el){
return function(event){if(event.keyCode==13) func1(event,el);};
};
for(var i=childs.length-1;i>=0;i--){
var child=childs.item(i);
try{
child.addEventListener('keyup',tmpEvent(child),true);
}
catch(ex){
child.attachEvent('onkeyup',tmpEvent(child),true);
}
}
}
</script>
</head><body onload="addTextEvent();">
<form action='http://www.baidu.com'>
<input type="text" /><br/>
<input type="text" /><br/>
<input type="text" /><br/>
<input type="text" /><br/>
<input type="text" /><br/>
<input type="text" /><br/>
<input type="text" /><br/>
</form>
</body>
</html>
var form=document.getElementsByTagName('form')[0],texts=form.getElementsByTagName('input');
for(var i=0,il=texts.length;i<il;i++){
var text=texts.item(i);
if(text.type!='text') continue;//增加判断是否为文本框,非文本框不做处理
if(ob!=text && i<il-1) continue;
if(ob==text && i<il-1){
texts.item(i+1).focus();
return false;
}
if(i==il-1)form.submit();
}
根据你们的代码 我改了一下,一路回车,光标都能跳到下一个文本框,但是在 最后一个“备注”文本框“beizhu” 的时候不能提交,运行出错:“行:24. 对象不支持此属性或方法”。
我的代码如下:<script language="javascript" type="text/javascript">
function func1(obj){
var e=window.event;
if(e.keyCode==13){
var id=obj.id;
if(id=='beizhu'){
form1.submit();//这是24行,估计就是这一行出错。
}else{ if (id=='shengchandate'){
document.all.yuangong.focus();
} if(id=='yuangong')
{
document.all.chanpinname.focus();
}
if(id=='danwei')
{
document.all.jian.focus();
}
if(id=='jian')
{
document.all.danjia.focus();
} if(id=='danjia')
{
document.all.beizhu.focus();
} }
}
}</script>
<table width="973" border="0" align=center cellpadding="6" cellspacing="1" class="tableBorder" id="form1">
<form action="caoqiu_save.asp" onSubmit="return chk_caoqiu()" method=post id=form1 name=form1 >
<tr bgcolor=ffffff>
<th height=25 colspan="17" align="center">添加生产记录--草球</th>
</tr>
<tr bgcolor=ffffff>
<td width="7%" align="center" class=forumrow>日期</td>
<td width="13%" align="center" class=forumrow>员工</td>
<td width="34%" align="center" class=forumrow>产品名称</td>
<td width="12%" align="center" class=forumrow>规格</td>
<td width="4%" align="center" class=forumrow>单位</td> <td width="4%" align="center" class=forumrow><p>件</p></td>
<td width="5%" align="center" class=forumrow><p>单价</p></td> <td width="21%" align="center" class=forumrow>备注</td>
</tr>
<tr bgcolor=ffffff>
<td align="center" class=forumrow><input name="shengchandate" type="text" id="shengchandate" size="10" value=<%=shengchandate%> onkeyup="func1(this);" ></td>
<td align="center" class=forumrow><input name="yuangong" type="text" id="yuangong" size="20" onkeyup="func1(this);" > </td>
<td align="center" class=forumrow><input name="chanpinname" size="60" type="text" id="chanpinname" autocomplete="off" onFocus="Suggest.Search(this,'caoqiu_suggest.asp');" onDblClick="JavaScript:window.open('select_proudt.asp?form=form1&chanpinname=chanpinname&guige=guige&danwei=danwei&ptypeid=ptypeid','','directorys=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=853,height=470,top=176,left=161');" /> </td>
<td align="center" class=forumrow><input name="guige" type="text" id="guige" size="20"></td>
<td align="center" class=forumrow><label>
<input name="danwei" type="text" id="danwei" size="4" onkeyup="func1(this);">
<input name="ptypeid" type="hidden" >
</label></td>
<td align="center" class=forumrow><input name="jian" type="text" id="jian" onKeyPress="JHshNumberText()" size="4" onkeyup="func1(this);" ></td>
<td align="center" class=forumrow><input name="danjia" type="text" id="danjia" size="4" onkeyup="func1(this);" ></td>
<td align="center" class=forumrow><label>
<input name="beizhu" type="text" id="beizhu" size="20" onkeyup="func1(this);">
</label></td>
</tr>
<tr bgcolor=ffffff>
<td colspan="17" align="center" class=forumrow><label>
</label></td>
</tr>
</form>
</table>期待最后的解决,再次感谢大家~!!!
window.document.forms[0] 具体怎么用?
但是改成 10楼的 代码 不知道为什么 就不行呢?
<form id="form1">
<input id="inp1" onkeyup="func1(this);"/><br/>
<input id="inp2" onkeyup="func1(this);"/><br/>
<input id="inp3" onkeyup="func1(this);"/><br/>
<input id="inp4" onkeyup="func1(this);"/><br/>
<input id="inp5" onkeyup="func1(this);"/><br/>
<input id="inp6" onkeyup="func1(this);"/><br/>
<input id="inp7" onkeyup="func1(this);"/><br/>
</form>
<script>
function func1(obj){
var e=window.event;
if(e.keyCode==13){
var id=obj.id;
var count=Number(id.replace("inp",""));
if(count==7){
form1.submit();
}else{
document.getElementById("inp"+(count+1)).focus();
}
}
}
</script>
问题在于 <table width="973" border="0" align=center cellpadding="6" cellspacing="1" class="tableBorder" id="form1">
id="form1" 对程序进行干扰,去掉就行了。
另外一个问题又出来了<form action="caoqiu_save.asp" onSubmit="return chk_caoqiu()" method=post id=form1 name=form1 >这里 onSubmit="return chk_caoqiu()" 失效了。这是我想检测表单数据合法性的。失效了,如果放在action="caoqiu_save.asp" 页面检查数据合法性,又时候数据不合法,返回的时候,不知道为什么文本框里输入的数据会丢失,要重新输入 就比较麻烦。
form1.submit();
}else{
document.getElementById("inp"+(count+1)).focus();
}
if(count==7){
if(chk_caoqiu()){
form1.submit();
}
}else{
document.getElementById("inp"+(count+1)).focus();
}
如果我的文本框不只7个,有些页面文本输入框多达10几个,然后前面几个是必须输入,后面有些是可输入可不输入的,当我想提交的时候,一直回车到最后一个文本框未免太麻烦(因为每天都得输入很多数据),有没有办法用组合键让它提前提交。比如按下CTRL+Enert,不用到最后一个文本框也可以提交。
if(e.keyCode==13){
var id=obj.id;
var count=Number(id.replace("inp",""));
if(count==7){
alert("提交了");
}else{
document.getElementById("inp"+(count+1)).focus();
}
}