你好,我正在页面上测试单选按钮,但它无法正常工作,否则为什么不进入 else if block ? 取消选中有效,但在选择单选按钮时无效
JQuery 代码
$("#s1").click(function () {
if ($("input[name=test]").prop("checked", false)) {
alert("Lütfen Seçin");
} else if ($("input[name=test]:checked").val() == "dunya") {
var x = parseInt($("#testt").text());
$.ajax({
url: "test.php",
data: { get_data: x },
type: "post",
success: function (e) {
alert("Tebrikler Doğru cevapladınız ve +10 puan kazandınız");
$("#testt").text(e);
},
});
} else {
alert("Yanlış Cevap Verdiniz Tekrar Kontrol Edin");
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" id="a" name="test" value="dunya">
<input type="radio" id="b" name="test" value="print">
<input type="radio" id="c" name="test" value="sayi">
<input type="button" value="Kontrol Et" class="first_btn" id="s1">
最佳答案
您正在检查第一个 if 语句中的错误值,并且您应该检查的是测试 input
的选中 radio
按钮,它总是会实现的,而不是整个单选按钮集的 props
:
$("input[name=test]:checked").val()
它应该是这样的:
$("#s1").click(function () {
if ($("input[name=test]:checked").val() === "sayi") {
console.log("INSIDE sayi check statement");
alert("Lütfen Seçin");
} else if ($("input[name=test]:checked").val() === "dunya") {
console.log("INSIDE dunya check statement");
var x = parseInt($("#testt").text());
$.ajax({
url: "test.php",
data: { get_data: x },
type: "post",
success: function (e) {
alert("Tebrikler Doğru cevapladınız ve +10 puan kazandınız");
$("#testt").text(e);
},
});
} else {
console.log("INSIDE print check statement");
alert("Yanlış Cevap Verdiniz Tekrar Kontrol Edin");
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" id="a" name="test" value="dunya">
<input type="radio" id="b" name="test" value="print">
<input type="radio" id="c" name="test" value="sayi">
<input type="button" value="Kontrol Et" class="first_btn" id="s1">
为了验证和检查用户是否选择了一个输入,您可以执行以下操作:
$("#s1").click(function () {
// Here we are checkinig if any radio button is selected.
if($("input[name=test]:checked").val() === undefined) {
alert('PLEASE SELECT A VALUE');
return ;
}
if ($("input[name=test]:checked").val() === "sayi") {
console.log("INSIDE sayi check statement");
alert("Lütfen Seçin");
} else if ($("input[name=test]:checked").val() === "dunya") {
console.log("INSIDE dunya check statement");
var x = parseInt($("#testt").text());
$.ajax({
url: "test.php",
data: { get_data: x },
type: "post",
success: function (e) {
alert("Tebrikler Doğru cevapladınız ve +10 puan kazandınız");
$("#testt").text(e);
},
});
} else {
console.log("INSIDE print check statement");
alert("Yanlış Cevap Verdiniz Tekrar Kontrol Edin");
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" id="a" name="test" value="dunya">
<input type="radio" id="b" name="test" value="print">
<input type="radio" id="c" name="test" value="sayi">
<input type="button" value="Kontrol Et" class="first_btn" id="s1">
关于javascript - 单选按钮检查不适用于 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61927630/