javascript - jQuery 点击事件停止工作

标签 javascript jquery

我有一个问题。 jQuery('#FinishTest').click() 有效,但当您选择所有答案时它会停止。该错误不会出现在控制台中。

bugs

我的代码

jQuery('#FinishTest').click(function() {

  var names = {};
  jQuery(':radio').each(function() {
    names[jQuery(this).attr('name')] = true;
  });
  var count = 0;
  jQuery.each(names, function() {
    count++;
  });

  if (jQuery(':radio:checked').length > 3) {
    if (jQuery(':radio:checked').length < count) {
      if (confirm("Henüz cevaplanmamış sorularınız var. Testi bitirmek istediğinizden eminmisiniz ?")) {
        finishtest();
      } else {
        jQuery('#FinishTest').bind('click');
        return;
      }
    }
  } else {
    alert('Testi bitirebilmek için en az 2 soru cevaplamalısınız.');
  }
});

最佳答案

发生这种情况是因为您的代码中存在以下情况:

if( jQuery(':radio:checked').length < count )

当您选择所有答案时,您将得到 jQuery(':radio:checked').length等于六 6count变量值也将是 6所以条件永远不会计算为 true。

尝试添加等于您的条件=所以它将低于或等于 <= :

if( jQuery(':radio:checked').length <= count ){
   //Your logic here
}

希望这有帮助。

更改并解决问题。

if (jQuery(':radio:checked').length > 3) {
  if (jQuery(':radio:checked').length < count) {
    if (confirm("Henüz cevaplanmamış sorularınız var. Testi bitirmek istediğinizden eminmisiniz ?")) {
      finishtest();
    }
  } else {
    finishtest();
  }
} else {
  alert('Testi bitirebilmek için en az 2 soru cevaplamalısınız.');
}

关于javascript - jQuery 点击事件停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47291420/

相关文章:

javascript - 想要使用 HTML5 的 Polyfill 来使日期类型在 Firefox 中工作

javascript - 解释 bindbind() 函数

javascript - 在 JS forEach 方法中包含 if..else... 语句?

javascript - 解析Cloudcode : putting functions into different files

连接到 MVC Controller 时 JavaScript POST 失败

javascript - window.open 有时会打开链接,有时不会

jquery - 如何在 javascript/jquery 中获取表单提交事件的响应?

javascript - 为什么它传递 console.log 作为 Observble subscribe() 函数的参数?

javascript - 如何知道 element.style 是从哪里应用到 CSS 中的

javascript - jquery升级到1.6.1后附加函数执行