jquery 捕获回车事件的实现代码

发布时间:2020-10-23编辑:脚本学堂
本文介绍下,jquery实现的捕获回车事件的一段代码,有用到的朋友,可以作个参考。

jquery捕获回车事件,记得引入jquery类库文件。

1,js代码:

function getEnter(obj,evt){
if(evt.keyCode == 13){
var target = $(":input").not(":radio,select,:reset");
for(var i=0;i<target.length;i++){
if(target.get(i).name==obj.name){
i++;
if(i==target.length-1){
$(target.get(i)).focus();
// 表单提交
$("form").submit(function(){
formSubmit();
return false;
});
}
$(target.get(i)).focus();
return false;
}
}
}
}

2,html代码:

<form name="consignBillForm"
action="consignBill/addConsignBillUIAction.action" method="post" >
运单号
<input  type="text" id="billCode" name="tconsignBill.billCode" onkeydown="getEnter(this,event);" title="运单号" />
<span class="billCode_errorInfo"></span><br>
货物编号
<input  type="text" id="goodsCode" name="tgoods.goodsCode" onkeydown="getEnter(this,event);" title="货物编号"/>
<span class="goodsCode_errorInfo"></span><br>
托运人姓名
<input type="text" class="sign" id="shipperName" name="tconsignBill.shipperName" onkeydown="getEnter(this,event);" title="托运人姓名"/>
<span class="shipperName_errorInfo"></span><br>
托运人电话
<input type="text" class="sign" id="shipperTel" name="tconsignBill.shipperTel" onkeydown="getEnter(this,event);" title="托运人电话"/>
<span class="shipperTel_errorInfo"></span><br>
取货人姓名
<input type="text" class="sign" id="receiverName" name="tconsignBill.receiverName" onkeydown="getEnter(this,event);" title="取货人姓名"/>
<span class="receiverName_errorInfo"></span><br>
取货人电话
<input type="text" class="sign" id="receiverTel" name="tconsignBill.receiverTel" onkeydown="getEnter(this,event);" title="取货人电话"/>
<span class="receiverTel_errorInfo"></span><br>
目的站
<select name="endStation">
<option>
1
</option>
</select>
<table border="1" width="500">
<th>
货物名称
</th>
<th>
件数
</th>
<th>
重量
</th>
<th>
体积
</th>
<th>
运费
</th>
<tr>
<td>
<input id="goodsName" type="text" name="tgoods.goodsName" onkeydown="getEnter(this,event);" title="货物名称"/>
<span class="goodsName_errorInfo"></span>
</td>
<td>
<input id="goodsCount" type="text" name="tgoods.goodsCount" onkeydown="getEnter(this,event);" title="货物件数"/>
<span class="goodsCount_errorInfo"></span>
</td>
<td>
<input id="goodsWeight" type="text" name="tgoods.goodsWeight" onkeydown="getEnter(this,event);" title="货物重量"/>
<span class="goodsWeight_errorInfo"></span>
</td>
<td>
<input id="goodsVolume" type="text" name="tgoods.goodsVolume" onkeydown="getEnter(this,event);" title="货物体积"/>
<span class="goodsVolume_errorInfo"></span>
</td>
<td>
<input id="goodsPrice" type="text" name="tgoods.price" onkeydown="getEnter(this,event);" title="货物运费"/>
<span class="goodsPrice_errorInfo"></span>
</td>
</tr>
<tr>
<td>
合计
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
</table>
代收货款
<input type="text" name="tconsignBill.insuranceCharge" onkeydown="getEnter(this,event);" title="代收货款"/>
保价费
<input type="text" name="tconsignBill.collectingMoney" onkeydown="getEnter(this,event);" title="保价费"/>
其他费用
<input type="text" name="tconsignBill.otherMoney" onkeydown="getEnter(this,event);" title="其他费用"/>
<br>
付款方式
<input type="radio" name="tconsignBill.typeOfPayment" value="已付" checked="checked"/>
已付
<input type="radio" name="tconsignBill.typeOfPayment" value="提付l"/>
提付
<input type="radio" name="tconsignBill.typeOfPayment" value="回单付"/>
回单付
<input type="radio" name="tconsignBill.typeOfPayment" value="月结"/>
月结
<input type="radio" name="tconsignBill.typeOfPayment" value="垫付"/>
垫付
<br>
垫付运费
<input type="text" id="advancementTransportationMoney" name="tconsignBill.advancementTransportationMoney" 
onkeydown="getEnter(this,event);"
title="垫付运费"/>
垫付货款
<input type="text" name="tconsignBill.advancementGoodsMoney" onkeydown="getEnter(this,event);" title="垫付货款"/>
回扣
<input type="text" name="tconsignBill.discountMoney" onkeydown="getEnter(this,event);" title="回扣"/>
<br>
备注
<textarea rows="6" cols="20" name="tconsignBill.remark" onkeydown="getEnter(this,event);"></textarea>
<span class="errorInfo" ></span>
<br>
<input type="button" value="保存打印" onclick="formSubmit()"/><input type="button" value="取消" />
<!--  
<input type="submit" value="保存打印" />
<input type="reset" value="取消" />
-->
</form>