0

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

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

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

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

错误控制台中没有错误。

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);  
4

1 回答 1

1

尝试

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

编辑:

您的脚本应该可以工作。把所有的和functions事件放在里面。handlerclick$(document).ready(function(){ //Code goes here });

还要确保jquery库是链接的。

于 2013-01-11T04:45:16.907 回答