基于JavaScript實(shí)現(xiàn)帶數(shù)據(jù)驗(yàn)證和復(fù)選框的表單提交
實(shí)現(xiàn):
1.用戶至少選中某項(xiàng),即表示選中該行,同時(shí)該行的數(shù)據(jù)驗(yàn)證通過,表單提交;否則,不提交。
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>帶數(shù)據(jù)驗(yàn)證和復(fù)選框的表單提交</title>
<script src="../commonJqery/jquery-3.0.0.js" type="text/javascript"></script>
<style type="text/css">
table {
border-collapse: collapse;
}
td,th {
width: 40px;
height: 100px;
border:1px solid #000;
}
table tbody tr:hover {
background-color: red;
}
table tbody tr:not(:first-child):hover {background-color: #666;
}
td input[name='number']{
width: 100px;
}
</style>
</head>
<body>
<form action="http://www.baidu.com" id="order_shopping" name="order_shopping" method="get" onsubmit="return checkShopping();">
<table id="table" class="fl">
<thead>
<tr>
<th>商品名</th>
<th>單價(jià)</th>
<th>購(gòu)買數(shù)量</th>
<th><input id="both" type="checkbox" name="both" autocomplete="off"></th>
</tr>
</thead>
<tbody>
<tr>
<td>香蕉</td>
<td>100</td>
<td><input type="text" name="number" autocomplete="off" placeholder="請(qǐng)輸入數(shù)量"></td>
<td>
<input type="checkbox" name="choice" autocomplete="off">
</td>
</tr>
<tr>
<td>蘋果</td>
<td>100</td>
<td><input type="text" name="number" autocomplete="off" placeholder="請(qǐng)輸入數(shù)量"></td>
<td>
<input type="checkbox" name="choice" autocomplete="off">
</td>
</tr>
</tbody>
</table>
<input type="submit" id="add_shopping" value="添加購(gòu)物車"/>
</form>
<p id="msg"></p>
</body>
</html>
js:
<script type="text/javascript">
$(function(){
//全選
$("input[name='both']").click(function(){
var $isSelected = $(this).is(":checked");
for(var i = 0;i<$("input[name='choice']").length;i++){
$("input[name='choice']")[i].checked = $isSelected;
}
})
});
// 輸入正確,且有選中該行復(fù)選框才提交
function checkShopping(){
$("#msg").html('');
var $checkbox = $("input[name='choice']");
var reg = /^[1-9]\d*$/;
var $number = "";
var isInteger = false;//記錄數(shù)字是否正確
var moreOne = false;//選擇復(fù)選框個(gè)數(shù)
$checkbox.each(function(){
if($(this).is(":checked")){
$number = $(this).parent().prev().children().val();
if(reg.test($number)){
isInteger = true;
moreOne = true;
}else{
$("#msg").html('提示:輸入數(shù)量必須為正整數(shù)');
isInteger = false;
}
}
})
if(isInteger && moreOne){
return true;
}else if(!moreOne){
$("#msg").html('提示:至少選擇一條信息');
return false;
}else{
return false;
}
}
</script>
總結(jié)
以上所述是小編給大家介紹的基于JavaScript實(shí)現(xiàn)帶數(shù)據(jù)驗(yàn)證和復(fù)選框的表單提交,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
window.open以post方式將內(nèi)容提交到新窗口
最近在做web項(xiàng)目,碰到需要跨頁(yè)面?zhèn)鬟f參數(shù)的功能,就是那種需要把當(dāng)前頁(yè)面的內(nèi)容帶到新開的子窗體中,以前的做法是傳一個(gè)id過去,然后在新窗口中去讀數(shù)據(jù)庫(kù)的內(nèi)容;比較有意思的是直接通過調(diào)用form的submit方法不能觸發(fā)onsubmit事件,查看了幫助文檔,必須手動(dòng)的觸發(fā),否則只能看到頁(yè)面刷新而沒有打開新窗口2012-12-12
一道面試題引發(fā)的對(duì)javascript類型轉(zhuǎn)換的思考
本文主要介紹了javascript類型轉(zhuǎn)換的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03
原生JS控制多個(gè)滾動(dòng)條同步跟隨滾動(dòng)效果
本文要探討的是,當(dāng)這兩個(gè)容器元素的內(nèi)容都超出了容器高度,即都出現(xiàn)了滾動(dòng)框的時(shí)候,如何在其中一個(gè)容器元素滾動(dòng)時(shí),讓另外一個(gè)元素也隨之滾動(dòng)2017-12-12
js實(shí)現(xiàn)簡(jiǎn)單的可切換選項(xiàng)卡效果
這篇文章主要介紹了js實(shí)現(xiàn)簡(jiǎn)單的可切換選項(xiàng)卡效果的方法,涉及javascript操作css樣式實(shí)現(xiàn)切換選項(xiàng)卡的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
js下關(guān)于onmouseout、事件冒泡的問題經(jīng)驗(yàn)小結(jié)
第3次遇到這個(gè)問題,于是總結(jié)了一下,將此短文發(fā)在首頁(yè),希望對(duì)瀏覽器事件機(jī)制有所了解的大俠們給予解答2010-12-12
JavaScript中使用stopPropagation函數(shù)停止事件傳播例子
這篇文章主要介紹了JavaScript中使用stopPropagation函數(shù)停止事件傳播例子,即阻止事件冒泡的一個(gè)方法,需要的朋友可以參考下2014-08-08
windows系統(tǒng)下簡(jiǎn)單nodejs安裝及環(huán)境配置
相信對(duì)于很多關(guān)注javascript發(fā)展的同學(xué)來說,nodejs已經(jīng)不是一個(gè)陌生的詞眼,這里不想談太多的nodejs的相關(guān)信息。只說一下,windows系統(tǒng)下簡(jiǎn)單nodejs環(huán)境配置2013-01-01

