我可以做一个可以调用现有Javascript函数的jQuery函数吗?但是,该Javascript函数来自不同的文件。
首先,我有一个按钮,它将打开包含表单的模式对话框。在表单弹出窗口之前,我想首先使用已经存在的函数检查验证。
<body>
<input type="button" name="buttonform" id="buttonform" value="Open Modal Form" />
<div id="dialogform" align = "center">
DIALOG
<form name="myForm2" action="/action_page.php" onsubmit="return validateForm()" method="post">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</div>
</body>
j查询功能:
<script>
$(function openform(){
$("#dialogform").dialog({
modal: true,
autoOpen: false,
title: "Modal Form",
width: 700,
height: 400,
});
$('#buttonform').click(function (){
$('#functioncheckheader')//function to call from other javascript file
$('#dialogform').dialog('open');
});
})
</script>
来自不同文件的javascript函数示例:
function checkheader(){
if($('#company').val() == 'select')
{ alert("Please select the date in header"); }
else if($('#project').val() == 'select')
{ alert("Please select the project in header"); }
else if($('#reqType').val() == 'select')
{ alert("Please select the request type"); }
else if($('#la').val() == "")
{ alert("Please fill up the LA no."); }
else if($('#workScope').val() == "")
{ alert("Please select the work scope."); }
}
顺便说一句,我是jQuery的新手。谢谢!!
首先,在jQuery之后将js文件添加到HTML中
<html>
<head>
<script src="functionToUse.js"></script>
</head>
</html>
如果您希望在jQuery代码中使用该文件中的函数。
$('#buttonform').click(function (){
funcFromFile(); //function to call from other javascript file
$('#dialogform').dialog('open');
});