javascript - 单选按钮检查不适用于 jquery

标签 javascript html jquery

你好,我正在页面上测试单选按钮,但它无法正常工作,否则为什么不进入 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/

相关文章:

javascript - Highcharts areaspline,没有获得图表的默认颜色

JavaScript/JQuery 函数不返回任何内容

javascript - 链接可以在 Cypress 中抽象出来吗?

javascript - 如何正确使用*ngIf?

javascript - 制作一个 JQuery 计算器,更改 innerHTML 但没有任何反应

html - ie7 中的悬停问题

javascript - 访问渲染的项目

javascript - 如何使用javascript防止屏幕休眠

javascript - Backstopjs 无法连接它启动的浏览器

javascript - 在没有jquery的情况下,将类添加到javascript中指定元素的所有子级(第一级或层次结构中)