gpt4 book ai didi

javascript - 根据定位运行不同的功能

转载 作者:行者123 更新时间:2023-11-29 10:37:27 26 4
gpt4 key购买 nike

enter image description here

所以基本上我有两个功能。如果我选中了当前选中的单选按钮之前,则第一个函数运行。如果我选中当前选中的单选按钮之后,则第二个函数运行。这是怎么做到的?

<input type="radio" id="a" name="radio">
<label for="a">A</label>

<input type="radio" id="b" name="radio" checked>
<label for="b">B</label>

<input type="radio" id="c" name="radio">
<label for="c">C</label>

<input type="radio" id="d" name="radio">
<label for="d">D</label>



$("input[name='radio']").change(function(){

if {
// do something
}

if {
// do something
}

});

最佳答案

检查之前输入的index,如果小于则触发前函数,否则触发后函数。然后将当前索引重置为现在的索引。

var currentIndex = $("input[name='radio']:checked").index('input');
$("input[name='radio']").change(function() {
var index = $(this).index('input');
if (index < currentIndex) {
console.log('before');
} else {
console.log('after');
}
currentIndex = index;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="radio" id="a" name="radio">
<label for="a">A</label>

<input type="radio" id="b" name="radio" checked>
<label for="b">B</label>

<input type="radio" id="c" name="radio">
<label for="c">C</label>

<input type="radio" id="d" name="radio">
<label for="d">D</label>

关于javascript - 根据定位运行不同的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34433335/

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