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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么利用js代码选中其中一个radio单选按钮,然后跳到指定网页。相关的知识,希望对你有一定的参考价值。

比如我选部门管理,然后点登陆跳到指定的页面?

需要准备的材料分别有:电脑、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页面。

参考技术A <script type="text/javascript">
function login()
    var portals = document.getElementsByName("portal");
    for(var i = 0; i < portals.length; i++)
        if(portals[i].checked)
            if(portals[i].value === "department")
                location.href = "部门管理地址";
            
            else if(portals[i].value === "content")
                location.href = "内容管理地址";
            
        
    

</script>


<input type="radio" name="portal" value="department" /> 部门管理
<input type="radio" name="portal" value="content" /> 内容管理
<input type="button" value="登录" onclick="login()" />

本回答被提问者采纳
参考技术B ..............$(button).click(function()window.location=$("radio:checked").val();)
把你要跳转的路径放到radio的value属性应该就可以了
参考技术C 单选框value给个 跳转页面的地址 点登录在js里获取地址 然后loction那个地址就OK了

checbox复选框实现radio单选框的单选功能

checbox复选框实现radio单选框的单选功能:
大家知道复选框可以一次选中多个,单选按钮每次只能够选中其中的一个,但是单选按钮比较霸道,你选中以后,只能够且必须选中其中一个,所有下面就通过checkbox复选框模拟实现单选按钮的功能,但是能够取消选中的项。
代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>蚂蚁部落</title>
<style>
span, input{
  float:left;
}
input{
  width:14px;
  height:14px;
}
span{
  margin-right:20px;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript"> 
$(function(){ 
  $(":checkbox").click(function(){ 
    if($(this).attr("checked")!=undefined){ 
      $(this).siblings().attr("checked",false); 
    } 
  }) 
}) 
</script>
</head>
<body>
<div>
  <input type="checkbox" />
  <input type="checkbox" />
  <input type="checkbox" />
  <input type="checkbox" />
  <input type="checkbox" />
  <input type="checkbox" />
</div>
</body>
</html>

上面的代码实现了我们想要的功能,下面介绍一下它的实现过程。
一.代码注释:
1.$(function(){}),文档结构完全加载完毕再去执行函数中的代码。
2.$(":checkbox").click(function(){}),为checkbox复选框注册click事件处理函数。
3.if($(this).attr("checked")!=undefined){},判断当前复选框是否被选中,因为如果复选框没有被选中attr()函数的返回值是undefined,如果选中的话返回值checked。
4.$(this).siblings().attr("checked",false),如果当前点击的被选中了,那么就将其兄弟复选框全部取消选中。
二.相关阅读:
1.attr()函数可以参阅jQuery的attr()方法一章节。 
2.siblings()函数可以参阅jQuery的siblings()方法一章节。

原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=14449

更多内容可以参阅:http://www.softwhy.com/jquery/

以上是关于怎么利用js代码选中其中一个radio单选按钮,然后跳到指定网页。的主要内容,如果未能解决你的问题,请参考以下文章

JQuery判断radio(单选框)是否选中和获取选中值方法总结

vuejs中怎么实现按钮组单选

radio 怎么默认选中上次选的那个按钮?

js怎么获取radio的值

js怎么实现点击选中,再次点击取消。

jquery循环遍历radio单选按钮,并设置选中状态