Jquery 选择上一个单选按钮值而不是当前值?
Jquery selecting previous radio button value and not current?
我正在尝试获取所选单选按钮的当前值,但它一直获取前一个按钮而不是当前按钮。您必须双击它才能获得当前按钮。
看了前面的回答还是不懂,求大神指点,谢谢。
码笔https://codepen.io/AdamChicago/pen/oNXZypq
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>D</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<button class="btn btn-dark" type="button" data-toggle="collapse" data-target="#collapseExample3" aria-expanded="false" aria-controls="collapseExample">
BUTTON
</button>
<div class="collapse" id="collapseExample3">
<div class="card card-body" style="background-color: rgb(65, 72, 78); border: none">
<div id="SortBTNS" class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-outline-info">
<input type="radio" name="options" id="SortPopular" value = "MostPopular" autocomplete="off"> Most Popular
</label>
<label class="btn btn-outline-info">
<input type="radio" name="options" id="SortTop" value = "TopRated" autocomplete="off"> Top Rated
</label>
<label class="btn btn-outline-info">
<input type="radio" name="options" id="SortTrending" value = "Trending" autocomplete="off"> Trending
</label>
<label class="btn btn-outline-info active">
<input type="radio" name="options" id="SortAZ" value = "A-Z" autocomplete="off" chedcked> A - Z
</label>
</div>
</div>
</div>
</body>
<script>
$('#SortBTNS').on('click', function() {
console.log($('#SortBTNS label.active input').val())
})
</script>
</html>
改用此代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>D</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<button class="btn btn-dark" type="button" data-toggle="collapse" data-target="#collapseExample3" aria-expanded="false" aria-controls="collapseExample">
<i class="fas fa-sort"></i>
</button>
<div class="collapse" id="collapseExample3">
<div class="card card-body" style="background-color: rgb(65, 72, 78); border: none">
<div id="SortBTNS" class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-outline-info">
<input type="radio" name="options" id="SortPopular" value = "MostPopular" autocomplete="off"> Most Popular
</label>
<label class="btn btn-outline-info">
<input type="radio" name="options" id="SortTop" value = "TopRated" autocomplete="off"> Top Rated
</label>
<label class="btn btn-outline-info">
<input type="radio" name="options" id="SortTrending" value = "Trending" autocomplete="off"> Trending
</label>
<label class="btn btn-outline-info active">
<input type="radio" name="options" id="SortAZ" value = "A-Z" autocomplete="off" chedcked> A - Z
</label>
</div>
</div>
</div>
</body>
<script>
$('#SortBTNS').on('click', function(e) {
console.log(e.target.children[0].value)
})
</script>
</html>
我正在尝试获取所选单选按钮的当前值,但它一直获取前一个按钮而不是当前按钮。您必须双击它才能获得当前按钮。
看了前面的回答还是不懂,求大神指点,谢谢。
码笔https://codepen.io/AdamChicago/pen/oNXZypq
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>D</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<button class="btn btn-dark" type="button" data-toggle="collapse" data-target="#collapseExample3" aria-expanded="false" aria-controls="collapseExample">
BUTTON
</button>
<div class="collapse" id="collapseExample3">
<div class="card card-body" style="background-color: rgb(65, 72, 78); border: none">
<div id="SortBTNS" class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-outline-info">
<input type="radio" name="options" id="SortPopular" value = "MostPopular" autocomplete="off"> Most Popular
</label>
<label class="btn btn-outline-info">
<input type="radio" name="options" id="SortTop" value = "TopRated" autocomplete="off"> Top Rated
</label>
<label class="btn btn-outline-info">
<input type="radio" name="options" id="SortTrending" value = "Trending" autocomplete="off"> Trending
</label>
<label class="btn btn-outline-info active">
<input type="radio" name="options" id="SortAZ" value = "A-Z" autocomplete="off" chedcked> A - Z
</label>
</div>
</div>
</div>
</body>
<script>
$('#SortBTNS').on('click', function() {
console.log($('#SortBTNS label.active input').val())
})
</script>
</html>
改用此代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>D</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<button class="btn btn-dark" type="button" data-toggle="collapse" data-target="#collapseExample3" aria-expanded="false" aria-controls="collapseExample">
<i class="fas fa-sort"></i>
</button>
<div class="collapse" id="collapseExample3">
<div class="card card-body" style="background-color: rgb(65, 72, 78); border: none">
<div id="SortBTNS" class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-outline-info">
<input type="radio" name="options" id="SortPopular" value = "MostPopular" autocomplete="off"> Most Popular
</label>
<label class="btn btn-outline-info">
<input type="radio" name="options" id="SortTop" value = "TopRated" autocomplete="off"> Top Rated
</label>
<label class="btn btn-outline-info">
<input type="radio" name="options" id="SortTrending" value = "Trending" autocomplete="off"> Trending
</label>
<label class="btn btn-outline-info active">
<input type="radio" name="options" id="SortAZ" value = "A-Z" autocomplete="off" chedcked> A - Z
</label>
</div>
</div>
</div>
</body>
<script>
$('#SortBTNS').on('click', function(e) {
console.log(e.target.children[0].value)
})
</script>
</html>