觸發Change事件
因為 id 不可以重覆,因此使用 div 包著 radio
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<div id="insuranceTypeSelect">
<input type="radio" name="insuranceType" value="0"/>機車
<input type="radio" name="insuranceType" value="1"/>汽車
</div>
</body>
</html>
<script type="text/javascript">
$(document).ready(function (){
$("#insuranceTypeSelect").change(function (){
var insuranceTypeVar = $("input[name='insuranceType']:checked").val();
alert(insuranceTypeVar);
});
});
</script>
以上還可以優化如下,不只是可以單擊 radio 圓圈可以觸發,單擊文字部份時,可以實現 radio 的觸發事件
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<div id="insuranceTypeSelect">
<input type="radio" id="insuranceType1" name="insuranceType" value="0"/><label for="insuranceType1">机动车</label>
<input type="radio" id="insuranceType2" name="insuranceType" value="1"/><label for="insuranceType2">特种车</label>
</div>
</body>
</html>
<script type="text/javascript">
$(document).ready(function (){
$("#insuranceTypeSelect").change(function (){
var insuranceTypeVar = $("input[name='insuranceType']:checked").val();
alert(insuranceTypeVar);
});
});
</script>