单选按钮检查事件处理
在我的应用程序中,我需要一个无线电组,每当检查一个单选按钮时,就会发出警报,以便我可以使用jQuery将它的值发布到ajaxpost。
你能帮我一下,请问我怎样才能在jQuery中做到这一点?
尝试这样的事情:
$(function(){ $('input[type="radio"]').click(function(){ if ($(this).is(':checked')) { alert($(this).val()); } }); });
如果你给你的单选按钮一个类,那么你可以用$('.someclass')
替换代码$('input[type="radio"]')
$('.someclass')
。
2017年更新:嘿。 这是一个可怕的答案。 不要使用它。 在过去,这种类型的jQuery使用很常见。 它可能在那时起作用了。 只是阅读它,意识到它是可怕的,然后继续(或downvote或其他)对其他一个更好的今天的jQuery答案。
$("input[type=radio]").change(function(){ alert( $("input[type=radio][name="+ this.name + "]").val() ); });
HTML代码:
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。 适用于所有情况:点击控制,使用键盘,使用操纵杆,自动更改其他dinamicalyfunction的值等。
您可以使用JQuery的方法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 });
您可以为所需的类或ID更改选择器'[type="radio"]'
。
问候!
$("#expires1").click(function(){ if (this.checked) alert("testing...."); });