我有 3 个具有相同类的单选按钮,当我单击特定的单选按钮时,我需要的是,然后该单选按钮的值应该通过在脚本下方引用单选按钮的类名来提醒,这不符合我的要求。
if($("input[type='radio'].radioBtn").prop('checked')) {
var btntype = $("input[type='radio'].radioBtn:checked").val();
alert(btntype);
}
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<input type="radio" class="radioBtn" name="numbers" value="1" />1<br/>
<input type="radio" class="radioBtn" name="numbers" value="2" />2<br/>
<input type="radio" class="radioBtn" name="numbers" value="3" />3<br/>
</body>
</html>
click
每次有人单击输入值时,您都应该使用事件来触发您的函数。您可以在jQuery API Doc 中阅读有关事件的更多信息。
$(document).ready(function() {
$("#inputs").on('click', 'input[type="radio"]', function() {
alert($(this).val()); // this points to the clicked element
});
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div id="inputs">
<input type="radio" class="radioBtn" name="numbers" value="1" />1<br/>
<input type="radio" class="radioBtn" name="numbers" value="2" />2<br/>
<input type="radio" class="radioBtn" name="numbers" value="3" />3<br/>
</div>
</body>
</html>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句