我有两组简单的单选按钮,如下所示:
Yes<input type="radio" name="first" value="yes"> No<input type="radio" name="first" value="no"><br>
Yes<input type="radio" name="second" value="yes"> No<input type="radio" name="second" value="no">
<h4 class="result"></h4>
我会把它们设置成这样:
所以,我想通过更改事件将其变成jQuery,我该怎么做?
答案 0 :(得分:0)
首先,通过关闭输入标记来修复标记。然后:
$(function () {
$('input[type="radio"]').on("change", function () {
var firstChecked = $('input[type="radio"][name="first"]:checked').val();
var secondChecked = $('input[type="radio"][name="second"]:checked').val();
if (typeof firstChecked == "undefined" || typeof secondChecked == "undefined") return;
var result = firstChecked == "yes" && secondChecked == "yes" ? "High" : firstChecked == "no" && secondChecked == "no" ? "Low" : "Medium";
$('.result').text(result);
});
});