会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132359个问题
JAVA 全系列/第四阶段:网页编程和设计/Javascript 语言(旧) 529楼

问题一:

老师,我想在点击新增一行这个按钮时新增的一行可以自定义书名,作者,数量,但是我发现我写的代码只能自定义书名和作者,而且数量随着书名和作者的值变化而变化,如果先在书名里面写张三那么数量也变为张三,然后在作者里面写李四,这时候数量又变成了李四,老师这个我该怎么解决


问题二:

我在书名,作者和数量的input里面都调用了失去焦点的方法,为什么只有数量可以成功调用,写完之后鼠标点击其他地方文本框边框消失,而作者和书名却不行


问题动图如下:


GIF.gif



源码如下:


html:

<html>
	<head>
		<title>jQuery操作表格</title>
		<meta charset="UTF-8"/>
		<!--声明css代码域-->
		<style type="text/css">
			tr{
				height: 40px;
			}
		</style>
	 
	 	<script src="js/jquery-1.9.1.js" type="text/javascript" charset="utf-8"></script>
		
		<script src="js/gwc.js"	type="text/javascript" charset="UTF-8"></script>
		
	</head>
	<body>
		<h3>jQuery操作表格</h3>
		<hr />
		<input type="button" id="fx" value="反选" />
		<input type="button" id="addRow" value="新增一行" />
		<input type="button" id="delRow" value="删除行" />
		<input type="button" id="copyRow" value="复制行" />
		<table border="1px" cellpadding="10px" cellspacing="0" id="ta">
			<tr>
				<td width="50px"><input type="checkbox" name="chks" id="chks" value="1" /></td>
				<td width="200px">书名</td>
				<td width="200px">作者</td>
				<td width="200px">数量</td>
				<td width="200px">操作</td>
			</tr>
			<tr id="">
				<td><input type="checkbox" name="chk" id="" value="2"/></td>
				<td>《Java编程之道》</td>
				<td>wollo</td>
				<td>10</td>
				<td>
					<input type="button" name="aa" id="" value="修改数量"  onclick="change(this)"/>
					<input type="button" name="" id="" value="删除" onclick="del(this)"/>
				</td>
			</tr>
			<tr>
				<td><input type="checkbox" name="chk" id="" value="3" /></td>
				<td>《Python和我的故事》</td>
				<td>赵老师</td>
				<td>10</td>
				<td>
					<input type="button" name="" id="" value="修改数量" onclick="change(this)"/>
					<input type="button" name="" id="" value="删除" onclick="del(this)"/>
				</td>
			</tr>
			<tr>
				<td><input type="checkbox" name="chk" id="" value="4" /></td>
				<td>《web开发详解》</td>
				<td>张老师</td>
				<td>30</td>
				<td>
					<input type="button" name="" id="" value="修改数量" onclick="change(this)"/>
					<input type="button" name="" id="" value="删除" onclick="del(this)"/>
				</td>
			</tr>			
		</table>
	</body>
</html>


JS:

$(function(){
	
	//确定全选和全部不选操作
	
	$("#chks").click(function(){
		
		var flag = $(this).prop("checked");//返回布尔类型
		
			$("input[name=chk]").prop("checked",flag);//让所有内容都选中

	})
	
	//判断是否全选的操作
	$("input[name=chk]").click(function(){
		
		var flag = true;
		
		var chk = $("input[name=chk]");
		
		//each:遍历		每次遍历执行funaction 里面的内容
		chk.each(function(){
			
			if(!$(this).prop("checked")){
				
				flag = false;
				
				return;
			}
			
		})
		
		$("#chks").prop("checked",flag);
		
	})
	
	//反选的操作
	$("#fx").click(function(){
		
		//拿到所有的多选框
		var chx = $("input[name=chk]");
		
		//遍历
		chx.each(function(){
			
			//获得每个多选框的初始状态
			var flag = $(this).prop("checked");
			
			//现有状态与原始状态进行取反
			$(this).prop("checked",!flag);
		})
	})
	
	//新增加一行的操作
	$("#addRow").click(function(){
		
		//获得tab对象
		var tab = $("#ta");
		
		tab.append('<tr id="">'+
				'<td><input type="checkbox" name="chk" id="" value="2"/></td>'+
				'<td><input type="text"	onblur="bul(this)"/></td>'+
				'<td><input type="text"	onblur="bul(this)"/></td>'+
				'<td><input type="text"	onblur="bul(this)"/></td>'+
				'<td>'+
					'<input type="button" name="aa" id="" value="修改数量"  onclick="change(this)"/>&nbsp'+
					'<input type="button" name="" id="" value="删除" onclick="del(this)"/>'+
				'</td>'+
			'</tr>');
		
	})
	
	//删除的操作
	$("#delRow").click(function(){
		
		//获得被选中的name=chk的单选框
		var del = $("input[name=chk]:checked");
		
		if(del.length==0){//如果一行没选
			alert("至少选择一行!");
		}else{
			
			//执行删除整行的操作	单选框的父节点是	td	,	td	的父节点是	tr
			del.parent().parent().remove();
		}
		
	})
	
	//复制行		选中几个复制几个
	$("#copyRow").click(function(){
		
		//获得被选中的name=chk的多选框
		var copy = $("input[name=chk]:checked");
		
		if(copy.length==0){//如果一行没选
			alert("至少选择一行!");
		}else{
			//执行copy的操作
			
			//复制
			var tr = copy.parent().parent().clone();//克隆整行
			//黏贴
			$("#ta").append(tr);
			
		}
	})
	
})

//修改数量的操作

function change(th){//这里的this是JS类型的,得转换成JQ对象
	
	//$(th):转换成JQ对象		获得	tr	结点
	var par = $(th).parent().parent();
	
	//将写死的数量变成可以定义的文本框
	par.children().eq(3).html("<input	type='text' size='3px' onblur='bul(this)'/>");
}

function bul(th){
	
	var par = $(th).parent().parent();
	
	par.children().eq(3).html(th.value);//或者转成JQ对象再调用val来获得值
}

function del(th){
	
	var par = $(th).parent().parent();
	
	par.remove();
	
}


JAVA 全系列/第四阶段:网页编程和设计/Jquery(旧) 531楼

问题1:

        老师为什么只有用户名那一行的两个列设置了宽度为150px和400px却作用到了整个表单的列呢?我加上边框之后看的更明显了,不应该只作用在用户名的那一行吗?


问题2:

        设置行高为35px 仅仅是为了更美更吧


table源码如下:

<table align="center"	border="2px">
				
				<tr height="35px">
					<td width="150px">用户名:</td>
					<td width="400px">                                        <!--失去焦点时进行方法调用-->
						<input type="text" name="uname" id="uname" value="" alt="用户名" onblur="checkName()"/>
						<span id="uname_span">*用户名必须是3~5位的汉字</span>
					</td>
				</tr>
				
				<tr height="35px">
					<td>密码:</td>
					<td>
						<input type="password" name="pwd" id="pwd" value="" alt="密码" onblur="checkPwd(	)"/>
						<span id="pwd_span"></span>
					</td>
				</tr>
				
				<tr height="35px">
					<td>手机号:</td>
					<td>
						<input type="number" name="phone" id="phone" value="" alt="手机号" onblur="checkPhone()"/>
						<span id="phone_span"></span>
					</td>
				</tr>
				
				<tr height="35px">
					<td>邮箱:</td>
					<td>
						<input type="text" name="email" id="email" value="" alt="邮箱" onblur="checkEmail()"/>
						<span id="email_span"></span>
					</td>
				</tr>
				
				<tr height="35px">
					<td>性别:</td>
					<td>
						男:<input type="radio" name="sex" id="" value="0" onclick="checkSex()"/>
						女:<input type="radio" name="sex" id="" value="1" onclick="checkSex()"/>
						<span id="sex_span"></span>
					</td>
				</tr>
				
				<tr height="35px">
					<td>爱好:</td>
					<td>
						<input type="checkbox" name="fav" id="" value="1" />抽烟
						<input type="checkbox" name="fav" id="" value="2" />喝酒
						<input type="checkbox" name="fav" id="" value="3" />烫头<br />
						<input type="checkbox" name="fav" id="" value="4" />泡妞
						<input type="checkbox" name="fav" id="" value="5" />撩妹
						<input type="checkbox" name="fav" id="" value="6" />吹牛
					</td>
				</tr>
				
				<tr height="35px">
					<td>籍贯:</td>
					<td>
						<!--默认值发生改变就是已选择,对应事件为onchange-->
						<select name="address" id="sel" onchange="checkAddress()">
							<option value="0">--请选择--</option>
							<option value="1">安徽</option>
							<option value="2">江苏</option>
							<option value="3">河南</option>
							<option value="4">河北</option>
							<option value="5">湖南</option>
							<option value="6">湖北</option>
						</select>
						<span id="sel_span"></span>
					</td>
				</tr>
				
				<tr height="35px">
					<td>验证码:</td>
					<td>
						<input type="number" name="yzm" id="yzm" value="" onblur="checkYZM()"/>
						<span id="yzm_span"></span>
						<span id="yzm2_span"></span>
						<input type="button" name="" id="" value="获取验证码"  onclick="YZM()"/>
					</td>
				</tr>
				
				<tr height="35px">
					<td>个人介绍:</td>
					<td>
						<textarea name="intro" rows="8" cols="30"></textarea>
					</td>
				</tr>
				
				<tr height="35px">
					<td colspan="2" align="center">
						<input type="checkbox" name="" id="check" value="" onclick="checkAgree()"/>是否同意本公司协议
					</td>
				</tr>
				
				<tr height="35px">
					<td align="center" colspan="2">
						<!--disabled="disabled"或者disabled="true"都表示不可用的-->
						<input type="submit" name="" id="sub" value="注册" disabled="disabled"/>	
					</td>
				</tr>
				
			</table>

实现效果如下:

image.png





JAVA 全系列/第四阶段:网页编程和设计/Javascript 语言(旧) 532楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637