[英]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. 我想要按钮执行的操作是触发
editvalidation()
函数,如果传递了该函数,则继续进行确认。 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? 我已经为验证设置了jquery代码,用于显示验证是否通过验证的确认,但是当单击
editvalidation()
如何获得返回return editvalidation()
按钮?
Below is relevant jquery code: 以下是相关的jQuery代码:
editvalidation(): 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(): 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...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.