我有一個html文件。在點擊按鈕的文件中,我想調用一個位於js文件中的函數。怎麼做?從html文件中的js文件調用函數
這裏是我的html文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Scrollable HTML table plugin for jQuery</title>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
<title>jQuery Mobile: Demos and Documentation</title>
<link rel="stylesheet" href="../jquery.mobile/jquery.mobile-1.1.0.css" />
<link rel="stylesheet" href="../docs/assets/css/jqm-docs.css" />
<link rel="stylesheet" href="../docsdemos-style-override.css" />
<script type="text/javascript" charset="utf-8" src="../cordova-1.6.1.js"></script>
<script type="text/javascript" src="../jquery.mobile/jquery-1.7.2.min"></script>
<script type="text/javascript" charset="utf-8" src="../js/main.js"></script>
<script type="text/javascript" src="../jquery.mobile/jquery.mobile-1.1.0.js"></script>
</head>
<body >
<!-- main login page -->
<div id="mypage" data-role="page" data-theme="a" data-ajax="false">
<div data-role="header" align="center">
<img border="0" src="../jquery.mobile/images/toplogo.png" />
</div>
<div data-role="content">
<form id="loginForm" >
<div data-role="fieldcontain" class="ui-field-contain ui-body ui-br" >
<label for="username">Username:</label> <input type="text"
name="username" id="username" value="" /> <label for="password">Password:</label>
<input type="password" name="password" id="password" value="" /> <label
for="dob">Date of birth:</label> <input type="password"
type="password" name="dob" id="dob" value="" />
</div>
<div class="ui-body ui-body-a" >
<fieldset class="ui-grid-a" data-theme="a" >
<div class="ui-block-a" data-theme="a">
<input type="submit" data-role="button" value="Login"
id="submitButton">
</div>
<div class="ui-block-b" data-theme="a">
<input type="reset" data-role="button" value="Cancel"
id="cancelButton">
</div>
</fieldset>
</div>
</form>
</div>
</div>
<!-- main menu -->
<script>
console.log("clickkkkkkkkkkkkkkkkkkkk");
$("#loginForm").on("submit",handleLogin);
</script>
此HTML文件可以通過www/UI文件夾中。現在在www/js文件夾下面有一個名爲main.js的js文件名。其中包含handleLogin函數。
function handleLogin()
{
}
如何從我的html文件中調用此函數。因爲當我點擊登錄按鈕時,相同的頁面被調用。任何幫助將不勝感激。提前致謝。
嗨Utkanos我已經添加evt在js文件中,如上所示,並且在$('#loginForm')。on('submit',handleLogin)上調用;從login.html仍然顯示相同的頁面。謝謝 – PPD 2012-07-07 10:58:16
假設你確定你的'handleLogin'函數正在調用(檢查這個)並且沒有JS錯誤,這不應該發生。如果不確定,請設置JS小提琴向我們展示。 – Utkanos 2012-07-07 11:01:45
這裏是[小提琴](http://jsfiddle.net/nT3hg/57/) – PPD 2012-07-07 11:14:45