请高手帮忙写下面这个功能的JS代码:
要求:
<button type="button" class="active">点击我</button>
<input type="radio" style="display: none">
点点击button按钮的class值为active的时候,input自动赋予check属性,即
<input type="radio" style="display: none" check>谢谢~~~
html
要求:
<button type="button" class="active">点击我</button>
<input type="radio" style="display: none">
点点击button按钮的class值为active的时候,input自动赋予check属性,即
<input type="radio" style="display: none" check>谢谢~~~
html
解决方案 »
- 页面间传递的参数无法获取JS&JSP
- 求一个在浏览器上非常平滑的跑马灯(滚动文字)的 js
- IE中的innerHtml问题,在JS中控制统计列表的表头动态改变,然后报未知的运行错误,谁有好的解决方案?
- xsl 如何实现累加
- style.display为空或者不是对象 急
- 急求正解,在线等待
- 问大家个问题,想学习网站开发,该如何下手?主要用什么技术?
- 跪求:用过sourceTec(一个用于生成导航条js的软件)的进!
- 请指教验证表单复选框问题?验证表单复选框是否选中并输出选中个数.
- 请问,在javaScript中,怎样跳出当前函数-----不运行后面的语句!
- js中模拟继承
- 怎样获取当前url某字段
<button type="button" class="active" onclick='checkedRadio(this)'>点击我</button>
<input type="radio" id="_radio" >
<script type="text/javascript">
function checkedRadio(obj){
document.getElementById('_radio').checked = obj.className=='active';
}
</script>
<button type="button" class="active" onclick='checkedRadio(this)'>点击我A</button>
<input type="radio" >A
<button type="button" class="active" onclick='checkedRadio(this)'>点击我B</button>
<input type="radio" >B
<input id="radio_a" type="radio" >A
<button type="button" class="active" onclick='checkedRadio(this,"radio_b")'>点击我B</button>
<input id="radio_b" type="radio" >B
<script type="text/javascript">
function checkedRadio(obj,_id){
document.getElementById(_id).checked = obj.className=='active';
}
</script>
还有一个小问题:
<button class="active">里面的active是通过其他JS控制,默认第一个有active值,当点击的时候,被激活的button才有active。所以,这里也要求<input type="radio">也是默认第一个有checked属性。谢谢你再帮忙
<input id="radio_a" type="radio" checked="true" >A
<button type="button" onclick='checkedRadio(this,"radio_b")'>点击我B</button>
<input id="radio_b" type="radio" >B
<script type="text/javascript">
function checkedRadio(obj,_id){
document.getElementById(_id).checked = obj.className=='active';
if(obj.className!='active'){
alert('激活了');
obj.className='active';
}
}
</script>
<input type="radio" name="post[regid]" value="6" id="g_6" checked="true">
<button type="button" name="post[regid]" value="5" id="g_5" onclick='checkedRadio(this,"g_5");' class="btn btn-warning btn-primary memtype" data-toggle="button">个人</button>
<input type="radio" name="post[regid]" value="5" id="g_5">function checkedRadio(obj,_id){
document.getElementById(_id).checked = obj.className=='active';
}
<button type="button" class="btn btn-warning btn-primary memtype active" onclick='checkedRadio(this,"g_6");' data-toggle="button">企业</button>
<input type="radio" name="post[regid]" value="6" id="g_6" checked="true">
<button type="button" name="post[regid]" value="5" id="g_5" onclick='checkedRadio(this,"g_5");' class="btn btn-warning btn-primary memtype" data-toggle="button">个人</button>
<input type="radio" name="post[regid]" value="5" id="g_5">
function checkedRadio(obj,_id){
document.getElementById(_id).checked = obj.className=='active';
}
<button type="button" class="btn btn-warning btn-primary memtype active" onclick='checkedRadio(this,"g_6");' data-toggle="button">企业</button>
<input type="radio" name="post[regid]" value="6" id="g_6" checked="true">
<button type="button" name="post[regid]" value="5" id="g_5" onclick='checkedRadio(this,"g_5");' class="btn btn-warning btn-primary memtype" data-toggle="button">个人</button>
<input type="radio" name="post[regid]" value="5" id="g_5">因为你的class比较多,className获取的是所有的class,所以一开始那个方法不行,加了个hasClass方法来判断。代码如下。<button type="button" class="btn btn-warning btn-primary memtype active"
onclick='checkedRadio(this,"g_6");' data-toggle="button">企业</button>
<input type="radio" name="post[regid]" value="6" id="g_6" checked="true">
<button type="button" name="post[regid]" value="5" onclick='checkedRadio(this,"g_5");' class="btn btn-warning btn-primary memtype" data-toggle="button">个人</button>
<input type="radio" name="post[regid]" value="5" id="g_5">
<script type="text/javascript">
function checkedRadio(obj,_id){
var _cheched = hasClass(obj,'active');
document.getElementById(_id).checked = _cheched;
if(!_cheched){
alert('激活');
obj.className+=' active';
}
}
function hasClass(obj , _class){
var _classes = obj.className.split(' ');
for(var i in _classes){
if(_classes[i]==_class){
return true;
}
}
return false;
}
</script>
这个可以。
最后一个小请求:
可不可以省去弹出“激活”的过程,也就是点击button的时候,直接radio被checked?
谢谢!!!!
可不可以省去弹出“激活”的过程
这个是你的问题,如果你不想要激活,直接把if判断那里的删除了,给button的class添加active就行了。
我要点击button的时候,radio自动激活。是这样写不?哪里有问题<script type="text/javascript">
function checkedRadio(obj,_id){
var _cheched = hasClass(obj,'active');
document.getElementById(_id).checked = _cheched;
obj.className+=' active';
}
function hasClass(obj , _class){
var _classes = obj.className.split(' ');
for(var i in _classes){
if(_classes[i]==_class){
return true;
}
}
return false;
}
</script>
<button type="button" class="btn btn-warning btn-primary memtype active"
onclick='checkedRadio(this,"g_6");' data-toggle="button">企业</button>
<input type="radio" name="post[regid]" value="6" id="g_6" checked="true">
<button type="button" name="post[regid]" value="5" onclick='checkedRadio(this,"g_5");' class="btn btn-warning btn-primary memtype" data-toggle="button">个人</button>
<input type="radio" name="post[regid]" value="5" id="g_5">
<script type="text/javascript">
function checkedRadio(obj,_id){
var _cheched = hasClass(obj,'active');
if(!_cheched){
obj.className+=' active';
}
document.getElementById(_id).checked = true;
}
function hasClass(obj , _class){
var _classes = obj.className.split(' ');
for(var i in _classes){
if(_classes[i]==_class){
return true;
}
}
return false;
}
</script>
<a href="#"><button type="button" id="but1" class="active" onclick='checkedRadio(this)'>点击我A</button></a>
<label for="btn1"><input type="radio" >A</label>
<a href="#"><button type="button" id="but1" class="active" onclick='checkedRadio(this)'>点击我B</button></a>
<label for="btn1"><input type="radio" >B</label>