繁体   English   中英

选择单选按钮值时显示警报

[英]Show alert when radio button value is selected

<script type="text/javascript">
function radio_form(){
   var selectvalue = $('input[name=choice]:checked', '#radio_form').val();

if(selectvalue == "V1"){
   alert('Value 1');
   return true;
}else if(selectvalue == "V2"){
   alert('Value 2');
   return true;
}else if(selectvalue == 'V3'){
   alert('Value 3');
   return true;
}else if(selectvalue == 'V4'){
   alert('Value 4');
   return true;
}
return false;
};
</script>

<form id="radio_form">
  <input type="radio" onclick="radio_form()"  name="choice" value="V1"> 
  <input type="radio" onclick="radio_form()" name="choice" value="V2"> 
  <input type="radio" onclick="radio_form()" name="choice" value="V3">
  <input type="radio" onclick="radio_form()" name="choice" value="V4"> 
</form>

我试图在选择单选值时显示警报...但是这种方法似乎不起作用。

难道我做错了什么?

您应该使用change事件而不是click事件。 更改事件仅在复选框的值更改时触发,单击事件将在您每次单击它们时触发,即使您单击已选中的复选框也是如此。 另外,不要使用内联事件,而只需使用jQuery选择器将事件附加到所有复选框。

 $("#radio_form input[type=radio]").change(function () { alert( 'Redirecting to: .../' + $(this).val() ); // This will redirect to the value, relative to the current path location.href = $(this).val(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="radio_form"> <input type="radio" name="choice" value="V1"> <input type="radio" name="choice" value="V2"> <input type="radio" name="choice" value="V3"> <input type="radio" name="choice" value="V4"> </form> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM