How to get radio button to display alert message

房东的猫 提交于 2021-02-11 15:34:21

问题


I want to get a radio button to display an alert when it is selcted, so the user can check that they have chosen the correct value. This is the code I have for the radio buttons:

 <tr>
     <td><input type="radio" name="level" id="GCSE" value="GCSE">GCSE<br>
    <input type="radio" name="level" id="AS" value="AS">AS<br>
    <input type="radio" name="level" id="A2" value="A2">A2</td>
 </tr>

How would I get so when any of these radio buttons are selected an alert is displayed to the user saying: "Are you sure you have chosen the correct entry level?"

Please keep answers simple, as I have only been learning HTML for 3 weeks


回答1:


You can do:

<input type="radio" name="level" id="GCSE" value="GCSE" onclick="alert('test');" />

Good luck!




回答2:


let's assume you have a form called # Myform

Then your code will be like this:

<form id="myForm">
<input type="radio" name="level" id="GCSE" value="GCSE" /> GCSE <br />
<input type="radio" name="level" id="AS" value="AS" /> AS <br />
<input type="radio" name="level"  id="A2" value="A2" /> A2 <br />
</form>

Then, put in a tag script something like this:

<script>
$('#myForm input').on('change', function() {
   alert("Are you sure you have chosen the correct entry level? +"$('input[name=level]:checked', '#myForm').val()); 
});
</scritp>

Hope this works for you!

Thx



来源:https://stackoverflow.com/questions/21431656/how-to-get-radio-button-to-display-alert-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!