[置顶] select,radio 回显不推荐使用jquery初始化

select,radio 表单回显避免使用jquery载入赋值

注意事项:

<html>
 <body>
<form method="post" action="">
		<!-- 如果表单中使用重置功能时,不推荐使用如下代码 -->
		<input type="radio" name="visible" value="1" />显示<br>
		<input type="radio" name="visible" value="0" />隐藏<br>
		<select name="orderBy" id="orderBy">
		   <option value="0">0</option>
		   <option value="1">1</option>
		</select><br>
		<input type="reset">
	</form>
 </body>
</html>

不推荐:使用如下js代码

 

<script type="text/javascript">
<!--
	$(function(){
	//回显时并不是真是数据的默认值
		$("input[type=radio][name=visible]").each(function() {
			if ($(this).val() == '${teacher.visible}') {
				$(this).attr("checked", "checked");
			}
		});
		$("#orderBy option").each(function() {
			if ($(this).val() == '${teacher.orderBy}') {
				$(this).attr("selected", "selected");
			}
		});
	});
//-->
</script>

 

最好的做法是:在jsp页面进行逻辑判断

 

<!-- 推荐使用如下代码 -->
<input type="radio" name="visible" value="1" <c:if test="${teacher.visible==1}">checked="checked"</c:if>/>显示<br>
<input type="radio" name="visible" value="0" <c:if test="${teacher.visible==0}">checked="checked"</c:if>/>隐藏<br>


 

 

 

posted @ 2013-06-08 20:26  jlins  阅读(1206)  评论(0编辑  收藏  举报