【问题标题】:PHP/HTML form and Javascript confirm boxPHP/HTML 表单和 Javascript 确认框
【发布时间】:2014-10-01 14:53:02
【问题描述】:

我使用LimeSurvey 工具创建了一个调查。对于一些自定义,我必须执行以下任务。

截图:

这里,当用户点击Next,如果有任何问题没有回答,我想通知用户他们错过了回答。

所以当他们点击 Next 时,我希望看到一个确认框 (Javascript),它会显示一条消息说您错过了。会有“没关系,进入下一页”按钮和“我会留在这里回答”按钮。

“没关系,进入下一页”按钮应该具有与“下一步”按钮相同的功能。

“我会留在这里并回答”按钮将让用户留在同一页面。

我知道可以使用 Javascript,但我不知道如何实现这个特定任务。

我只知道Next按钮的ID是“movenextbtn”。

但是,当我点击下一步按钮并使用确认框时,我将如何检查问题是否未被回答,我将如何进入下一页或留在同一页面。

任何帮助将不胜感激。

提前致谢。

【问题讨论】:

  • 类似stackoverflow.com/questions/14957604/… .. 但它根本没有帮助
  • 您必须提供更多信息。最低限度,单选按钮的类/ID
  • 只需将问题设为必填项,并在调查中设置不回答选项,无需过于复杂。
  • 我可以从 Firebug 中看到“answertext”是具有单选按钮问题的表的类。 “answer_cell_001 answer-item radio-item”是单选按钮的类
  • @Pwner 不,我们正在进行一项研究调查,不能要求人们回答,同时我们也没有准备好通知他们如果他们错误地遗漏了什么。

标签: javascript php jquery limesurvey


【解决方案1】:

将此脚本放在数组问题的源代码中:

<script type="text/javascript" charset="utf-8"> 
$(document).ready(function() { 

    // Identify this question
    var thisQuestion = $('#question'+{QID}+'');

    // Listener on the "Next" button
    $('#movenextbtn').click(function(event){
        // Compare number of clicked radios to number of array rows
        if($('input.radio:checked', thisQuestion ).length != $('tr.answers-list',thisQuestion ).length) {
            // Pop up a confirm message
            if(confirm('You have not answered all rows.\n\nAre you sure you want to continue?')) { 
                return true;
            }
            else { 
                return false;
            }
        }
    });
});
</script>

【讨论】:

  • 真正的救星在这里和 LimeSurvey 的论坛中。 Mucha gracias 表示敬意。
【解决方案2】:

这段代码假定您只对表单上的单选按钮感兴趣,该表单的按钮 ID 为 movenextbtn。您必须将其插入页面中的某个位置。

<script>                                                                        
  // wait for everything to load                                                
  window.addEventListener('load', function() {                                  
    // let's check radio buttons when the next button is clicked   
    var nextButton = document.getElementById('movenextbtn');                    
    var form = nextButton.form;                                                 

    nextButton.addEventListener('click', function(e) {                          
      var radioElements = form.querySelectorAll('input[type="radio"]');         
      var currentRadioName;                                                     
      var seenRadioName;                                                        
      var index;                                                                
      var confirmation;                                                         

      for(index = 0; index < radioElements.length; index += 1) {                
        currentRadioName = radioElements[index].name;                           

        // check if we've already seen this named radio button set              
        if(seenRadioName != currentRadioName) {                                 
          // check that there's a checked radio button for this set of named radio buttons
          if(!form.querySelectorAll('input[type="radio"][name="' + currentRadioName + '"]:checked').length) {
            // didn't answer a question, ask if that's ok                       
            confirmation = confirm("You have missed answering a question.\n\nOK to Continue?\nCancel form submission and answer?");

            if(confirmation) {                                                  
              // the user clicked ok, let's submit the form                     
              return true;                                                      
            }                                                                   
            else {                                                              
              // the user clicked cancel, let's stop the form submission        
              e.preventDefault();                                               
              e.stopPropagation();                                              
              return false;                                                     
            }                                                                   
          }                                                                     

          // remember that we already checked this set of radio buttons         
          seenRadioName = currentRadioName;                                     
        }                                                                       
      }                                                                         
    }, false);                                                                  
  }, false);                                                                    
</script>                                                                       

您可以在这里玩更多:http://jsbin.com/howuzapi/1/edit

【讨论】:

  • AlfredoDelgado ...它在一定程度上可以正常工作,但是在确认框之后,下一个按钮将不起作用,有什么想法可以修复它吗?
  • 你能说得更具体点吗?无论我已回答所有问题还是选择在未回答后确认,下一个按钮对我来说都很好。您的作品是否可以在我可以访问的公共 URL 上找到?你见过它在jsbin.com/howuzapi/1/edit 工作吗?
猜你喜欢
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 2016-09-22
  • 2013-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-10
相关资源
最近更新 更多