简体   繁体   中英

How to get a button to perform a function when it has been clicked

I have a button below:

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

What I want the button to do is to trigger the editvalidation() function and if that is passed then proceed with the confirmation. I have the jquery code set up for the validation and for showing the confirmation if the validation has passed but how do I get the button to return the editvalidation() whenc clicked on?

Below is relevant jquery code:

editvalidation():

        function editvalidation()
{
    if ($("#coursesDrop").val()==""){
        $("#courseAlert").html("Please Select a Course from the Course Drop Down Menu");
    }
    if ($("#coursesDrop").val()!="" && $("#courseadd").children().length==0){
        $("#courseAlert").html("You have not Selected any Students you wish to Add into Course");
    }
    if ($("#coursesDrop").val()!="" && $("#courseadd").children().length!=0){
        return true;
    }
    return false;
}

showConfirm():

         function showConfirm(){

          var courseNoInput = document.getElementById('currentCourse').value;
          var courseNameInput = document.getElementById('currentCourseName').value;

          if (editvalidation()) {

         var confirmMsg=confirm("Are you sure you want to add your selected Students to this Course:" + "\n" + "Course: " + courseNoInput + " - " + courseNameInput);

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

current even to check for click on button:

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

Have you tried

$('#submitstudents').on('click', function(){ if(editvalidation()) showConfirm(); });

?

Try this:

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

I've encountered this before. This code works perfect all the time:

$('#submitstudents').live('click', function(){
   ...do what ever you want...
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM