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>