javascript怎么选中radio

JavaScript08

javascript怎么选中radio,第1张

可以按照下面的代码来操作:

<html>

<head>

<script type="text/javascript" src="./jquery.min.js"></script>

</head>

<body>

<div>

<input id="rdo1" name="rdo1" type="radio" value="1" checked="checked"/>是

<input id="rdo1" name="rdo1" type="radio" value="0"/>否

<button id="btn1">是</button>

<button id="btn2">否</button>

<div>

<script type="text/javascript">

$(function(){

$("#btn1").click(function(){

$("input[name='rdo1']").eq(0).attr("checked","checked")

$("input[name='rdo1']").eq(1).removeAttr("checked")

$("input[name='rdo1']").eq(0).click()

})

$("#btn2").click(function(){

$("input[name='rdo1']").eq(0).removeAttr("checked")

$("input[name='rdo1']").eq(1).attr("checked","checked")

$("input[name='rdo1']").eq(1).click()

})

})

</script>

</body>

</html>

需要准备的材料分别有:电脑、html编辑器、浏览器。

1、首先,打开html编辑器,新建html文件,例如:index.html,填充问题基础代码。

2、在index.html中的<script>标签,输入js代码:

function fun() {

var a = $('input:radio:checked').val()

if (a == 1) {

location.href = 'page1.html'

} else {

location.href = 'page2.html'

}

}

3、浏览器运行index.html页面,选择内容管理,点击登录。

4、此时成功进入到了page2.html页面。