JQuery Radion 選中事件

JQuery radio checked 選中事件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>test</title>
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".radioItem").change(
function() {
var $selectedvalue = $("input[name='rl$tt']:checked").val();
alert($selectedvalue);
if ($selectedvalue == 1) {
window.location = "http://www.g.cn";
}
else {
window.location = "http://www.baidu.com";
}
});});
</script>
</head>
<body>
<table id="rl">
<tr>
<td><label>谷歌</label>
<input id="rl_1" class="radioItem" checked="checked" name="rl$tt" type="radio" value="1" />
<label>百度</label>
<input id="rl_2" class="radioItem" name="rl$tt" type="radio" value="2" /></td>
</tr>
</table>
</body>
</html>

如果是許多的radio那麼可以使用遍歷的方式

<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<html>
<head>
<script type="text/javascript" >
  function baidu(){
  var radios = document.getElementsByName("a");
  for ( var i = 0; i < radios.length; i++) {
  if (radios[i].checked==true) {
      i++;
   alert("你選中的是第"+i+"個單選框。值爲:"+radios[--i].value);
   }
  }
}
</script>
</head>
<body>
  <input type="radio" name="a" value="1"/>1
  <input type="radio" name="a" value="2"/>2
  <input type="radio" name="a" value="3"/>3
  <input type="radio" name="a" value="4"/>4
  <input type="button" onClick="baidu()" value="按下"/>
</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章