【问题标题】:Can I call an exist javascript function into jquery function我可以将现有的 javascript 函数调用到 jquery 函数中吗
【发布时间】:2018-04-16 15:57:04
【问题描述】:

我可以制作一个可以调用现有 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>

jQuery 函数:

<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 新手。谢谢!!

【问题讨论】:

  • 是的,你可以这样做
  • 使用checkheader() 而不是$('#functioncheckheader')
  • 这是可能的,只要checkheader在全球范围内公开。
  • 只要您使用&lt;link&gt;&lt;script&gt; 标签导入该文件。
  • 我明白了。再次感谢大家!

标签: javascript jquery jquery-ui-dialog


【解决方案1】:

首先,在 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');      
});

【讨论】:

  • 自定义 js 应该在 之后包含 jquery.js,因为它使用 jquery。
  • 感谢您发现@freedomn-m
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 1970-01-01
  • 2020-04-30
  • 2014-09-02
  • 2011-04-05
  • 2017-12-27
相关资源
最近更新 更多