从另一个页面将参数传递给文档就绪函数
pass paramenter to document ready function from another page
我将此代码放在单独的 js 文件中,无需重新加载页面即可将内容加载到页面中:
$(document).ready(function(){
//Catch the form's submit event:
$('#skipreload').submit(function() {
//Create AJAX call:
$.ajax({
//Get form data:
data: $(this).serialize(),
//GET or POST method:
type: 'GET',
//The file to call:
url: 'webaddress.php',
//On success update div with response:
success: function(response) {
$('#form_output').html(response);
}
});
return false;
});
});
我在页面中这样调用函数:
<html>
<body>
<head>
<script src="js/script.js">
<!-- Want to create variable to pass parameter to document ready function here -->
</script>
</head>
<body>
(...)
现在我想将 'webaddress.php'-url 作为参数从 HTML 页面传递到 script.js 文件及其提交函数,但是我不能使这项工作。有人知道如何做到这一点吗?
将其保存在 div 中并加载其值:
已编辑
<div id='url' style='display:none'>webaddress.php</div>
$(document).ready(function(){
var url = $("#url").text();
.
.
.
}
或
<input type='hidden' id='url' value='webaddress.php' />
$(document).ready(function(){
var url = $("#url").val();
.
.
.
}
正如@Satpal 已经提到的,您可以在 HTML 中创建一个全局变量,该变量可以在外部脚本中访问
<html>
<body>
<head>
<script>var myUrl = 'webaddress.php';</script>
<script src="js/script.js"></script>
</head>
<body>
(...)
在script.js中:
url: myUrl,
注意:在这种情况下,您一定不要忘记在每个使用 script.js 的 HTML 文件中提供此值,否则您将获得 "undefined" for myUrl.
我将此代码放在单独的 js 文件中,无需重新加载页面即可将内容加载到页面中:
$(document).ready(function(){
//Catch the form's submit event:
$('#skipreload').submit(function() {
//Create AJAX call:
$.ajax({
//Get form data:
data: $(this).serialize(),
//GET or POST method:
type: 'GET',
//The file to call:
url: 'webaddress.php',
//On success update div with response:
success: function(response) {
$('#form_output').html(response);
}
});
return false;
});
});
我在页面中这样调用函数:
<html>
<body>
<head>
<script src="js/script.js">
<!-- Want to create variable to pass parameter to document ready function here -->
</script>
</head>
<body>
(...)
现在我想将 'webaddress.php'-url 作为参数从 HTML 页面传递到 script.js 文件及其提交函数,但是我不能使这项工作。有人知道如何做到这一点吗?
将其保存在 div 中并加载其值:
已编辑
<div id='url' style='display:none'>webaddress.php</div>
$(document).ready(function(){
var url = $("#url").text();
.
.
.
}
或
<input type='hidden' id='url' value='webaddress.php' />
$(document).ready(function(){
var url = $("#url").val();
.
.
.
}
正如@Satpal 已经提到的,您可以在 HTML 中创建一个全局变量,该变量可以在外部脚本中访问
<html>
<body>
<head>
<script>var myUrl = 'webaddress.php';</script>
<script src="js/script.js"></script>
</head>
<body>
(...)
在script.js中:
url: myUrl,
注意:在这种情况下,您一定不要忘记在每个使用 script.js 的 HTML 文件中提供此值,否则您将获得 "undefined" for myUrl.