2013-01-11 59 views
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); 

回答

1

尝试

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

编辑:

你的脚本应该工作。将functionshandlerclick事件放入$(document).ready(function(){ //Code goes here });

还要确保jquery库已链接。

+0

没有,这并没有改变任何东西。必须是我的代码中的其他东西。当按钮没有做任何事情时,你认为有问题可能是你想要查看的某段代码吗?你想让我发布我的HTML表单吗? – user1964964

+0

修正了它。您在'submitform()'中错过了关闭'}'。 – dunli

+0

对不起,dunil,这是一个粘贴错误的SO,我确实有那个闭括号,对不起 – user1964964