gpt4 book ai didi

javascript - 改变 radio 上的反向 div

转载 作者:太空宇宙 更新时间:2023-11-03 21:30:31 25 4
gpt4 key购买 nike

当用户在输入 radio 上选择某些内容时,我想在一个更大的 div 中反转两个 div。

我有这个 html:

<input type="radio" name="orderBy" id="orderByAsc" value="asc" >
<input type="radio" name="orderBy" id="orderByDesc" value="desc" >
<div class="col-md-10">
<div class="tags">
//Stuff inside
</div>
<div class="tagsDifficile">
//Stuff inside
</div>
</div>

所以我在 javascript 中尝试过类似的东西:

$('input[type=radio]').change(function (){
valueRadio = this.value;
$('.col-md-10 > div').each(function() {
if(valueRadio == 'asc'){
$(this).prependTo(this.parentNode);
}else{
$(this).appendTo(this.parentNode);
}
});
});

但它不起作用,当我点击单选按钮时,div 没有移动。当我点击 radio asc 时它正在工作,但在 desc 上没有任何内容。非常感谢您的帮助。预先感谢您的回答。

最佳答案

$(document).on('change', 'input[name=orderBy]', function() {
var that = $(this);
var val = that.val();
var wrp = $('#wrp');
var one = $('#one');
var two = $('#two');
if (val == 'desc') {
var oneC = one.clone();
one.remove();
two.before(oneC);
} else if (val == 'asc') {
var twoC = two.clone();
two.remove();
one.before(twoC);
}
});
.tags {
width: 100%;
padding: 10px;
box-sizing: border-box;
color: #fff;
}
#one { background-color:green }
#two { background-color:red }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Asc: <input type="radio" name="orderBy" id="orderByAsc" value="asc" >
Desc: <input type="radio" name="orderBy" id="orderByDesc" value="desc" >
<div id="wrp" class="col-md-10">
<div id="one" class="tags">
//Stuff inside (one)
</div>
<div id="two" class="tags">
//Stuff inside (two)
</div>
</div>

关于javascript - 改变 radio 上的反向 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29694458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com