如何在单击按钮时获取按钮以执行功能

时间:2013-03-01 15:16:09

标签: javascript jquery html

我在下面有一个按钮:

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

我想要按钮做的是触发editvalidation()功能,如果通过,则继续确认。我已经为验证设置了jquery代码,并且如果验证已经通过则显示确认但是如何让按钮在点击时返回editvalidation()

以下是相关的jquery代码:

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();   
     }
  }
} 

当前甚至检查点击按钮:

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

3 个答案:

答案 0 :(得分:1)

你试过吗

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

答案 1 :(得分:0)

试试这个:

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

答案 2 :(得分:0)

我之前遇到过这个。这段代码一直很完美:

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