在我的应用程序中,我需要一个无线电组,每当检查无线电按钮时,就会发出警报,以便我可以将其价值发布到jquery的Ajax帖子中。
你能帮我请我如何在jQuery中做到这一点吗?
在我的应用程序中,我需要一个无线电组,每当检查无线电按钮时,就会发出警报,以便我可以将其价值发布到jquery的Ajax帖子中。
你能帮我请我如何在jQuery中做到这一点吗?
尝试这样的事情:
$(function(){
$('input[type="radio"]').click(function(){
if ($(this).is(':checked'))
{
alert($(this).val());
}
});
});
如果你给你的单选按钮一个类,那么你可以$('input[type="radio"]')
用$('.someclass')
.
2017 年更新:嘿。这是一个可怕的答案。不要使用它。在过去,这种类型的 jQuery 使用很常见。它可能在当时有效。只需阅读它,意识到它很糟糕,然后继续(或否决或其他)对今天的 jQuery 更好的其他答案之一。
$("input[type=radio]").change(function(){
alert( $("input[type=radio][name="+ this.name + "]").val() );
});
HTML 代码:
<input type="radio" name="theName" value="1" id="option-1">
<input type="radio" name="theName" value="2">
<input type="radio" name="theName" value="3">
Javascript代码:
$(document).ready(function(){
$('input[name="theName"]').change(function(){
if($('#option-1').prop('checked')){
alert('Option 1 is checked!');
}else{
alert('Option 1 is unchecked!');
}
});
});
在名为“theName”的多个收音机中,检测选项 1 何时被选中或未选中。适用于所有情况:点击控制、使用键盘、使用操纵杆、自动更改其他动态功能的值等。
您可以简单地使用change
JQuery 的方法来获取当前单选的值,并使用以下代码进行检查:
$(document).on('change', '[type="radio"]', function() {
var currentlyValue = $(this).val(); // Get the radio checked value
alert('Currently value: '+currentlyValue); // Show a alert with the current value
});
您可以更改所需'[type="radio"]'
的类或 ID 的选择器。
$("#expires1").click(function(){
if (this.checked)
alert("testing....");
});
$(document).on('change','.radio-button', function(){
const radio = $(this);
if (radio.is(':checked')) {
console.log(radio)
}
});