怎么利用js代码选中其中一个radio单选按钮,然后跳到指定网页。

JavaScript013

怎么利用js代码选中其中一个radio单选按钮,然后跳到指定网页。,第1张

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

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

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

functionfun(){

vara=$('input:radio:checked').val()

if(a==1){

location.href='page1.html'

}else{

location.href='page2.html'

}

}

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

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

设置radio的checked属性为ture即为选中。

<input type="radio" id="radioId" value="1" >

选中单选框JS代码:

var target = document.getElementById("radioId")

target.checked = true

附完整代码:

<!DOCTYPE html>

<!--STATUS OK-->

<html>

<head>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=Edge" />

<meta http-equiv="content-type" content="text/htmlcharset=gbk" />

<meta property="wb:webmaster" content="3aababe5ed22e23c" />

<meta name="referrer" content="always" />

<title>demo</title>

</head>

<body>

<input type="radio" value="radio1" id="radio1" name="radio"/><label for="radio1">RADIO 1</label>

<input type="radio" value="radio2" id="radio2" name="radio"/><label for="radio2">RADIO 2</label>

<input type="radio" value="radio3" id="radio3" name="radio"/><label for="radio3">RADIO 3</label>

<input type="radio" value="radio4" id="radio4" name="radio"/><label for="radio4">RADIO 4</label>

<br/><br/>

<button onclick="selectRadio('radio1')">选中radio1</button>

<button onclick="selectRadio('radio2')">选中radio2</button>

<button onclick="selectRadio('radio3')">选中radio3</button>

<button onclick="selectRadio('radio4')">选中radio4</button>

<br/>

<br/>

</body>

<script type="text/javascript">

function selectRadio(radioId) {

var target = document.getElementById(radioId)

target.checked = true

}

</script>

</html>

$(function(){

    $("li").click(function() {

        $(this).addClass('act')        

    })

})