【问题标题】:Clicking 'OK' in confirmation does not submit page在确认中单击“确定”不会提交页面
【发布时间】:2013-01-11 04:37:58
【问题描述】:

我的<body> 代码部分中有一个简单的按钮:

<p><button id='submitstudents'>Submit Students</button></p>

我想让按钮做的是,如果点击,它将通过editvalidation()函数查看是否有错误,如果没有错误则显示一个确认框,如果用户确认则它应该执行 ajax。

问题是当我点击确认框中的OK按钮时,什么也没有发生,页面甚至没有加载,我的问题是什么导致点击@后页面没有提交987654325@按钮在确认吗?

错误控制台中没有错误。

Jquery 代码(按正确顺序显示):

验证:

function editvalidation()
{
    if ($("#sessionsDrop").val()==""){
        $("#studentAlert").html("Please Select an Assessment to edit from the Assessment Drop Down Menu");
    }
    if ($("#sessionsDrop").val()!="" && $("#studentadd").children().length==0){
        $("#studentAlert").html("You have not Selected any Students you wish to Add into Assessment");
    }
    if ($("#sessionsDrop").val()!="" && $("#studentadd").children().length!=0){
        return true;
    }
    return false;
}

提交表格:

     function submitform() {    

        $.ajax({
            type: "POST",
            url: "updatestudentsession.php",
    data: { 
        sessionid:  $('#currentid').val(),
        students:   $('#addtextarea').val()
    },
            dataType:'json',  //get response as json
            success: function(result){
                        if(result.errorflag){

           //do your stuff on getting error message
          var newHtml="<span style='color: red'>"+result.msg+"</span>"; 
          $("#targetdiv").html(newHtml);  //i am displaying the error msg here

        }else{
           //you got success message

           var newHtml="<span style='color: green'>"+result.msg+"</span>"; 
                $("#targetdiv").html(newHtml);


               //append students you want to add to assessment into student exist select box 
               var selectedStudents = jQuery("select#studentadd").find("option");
                selectedStudents.appendTo($("select#studentexist"));
                $('option.red', 'select#studentexist').remove(); 

                 //blank #studentadd select box
                 $('#studentadd').empty(); 

                    $('#targetdiv').show();
            }
        }
      }); 
}

确认:

         function showConfirm(){

          var examInput = document.getElementById('currentAssessment').value;

          if (editvalidation()) {

         var confirmMsg=confirm("Are you sure you want to add your selected Students to this Assessment:" + "\n" + "Exam: " + examInput);

         if (confirmMsg==true)
         {
         submitform();   
     }
  }
}

按钮点击:

$('body').on('click', '#submitstudents', showConfirm);  

【问题讨论】:

    标签: jquery html


    【解决方案1】:

    试试

    $('#submitstudents').click(function(){
        showConfirm();
    });
    

    编辑:

    您的脚本应该可以工作。将click 事件的所有functionshandler 放在$(document).ready(function(){ //Code goes here }); 中。

    还要确保 jquery 库已链接。

    【讨论】:

    • 不,这并没有改变任何东西。必须是我的代码中的其他内容。当按钮不执行任何操作时,如果您认为问题可能出在哪里,您是否可能想查看某段代码?您希望我发布有问题的 HTML 表单吗?
    • 已修复。您的submitform() 中缺少一个结束}
    • 对不起,这是一个粘贴错误,我确实有那个右括号,对此感到抱歉
    • 如果是这种情况,您的代码应该可以正常工作。我已经更新了我的答案。我测试了你的脚本,它运行良好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    相关资源
    最近更新 更多