[英]How do I pass parameters to javascript from each page dynamically
The following code provides Remove capability on each of the list page (eg sample list page given below), of which only 3 variables (tableId, removeButtonId, headerCheckbox) vary in each page. 以下代码在每个列表页面(例如,下面给出的示例列表页面)上提供了Remove功能,其中每个页面中只有3个变量(tableId,removeButtonId,headerCheckbox)有所不同。 How do I re-use this script across all pages, instead of defining this in each page. 如何在所有页面上重用此脚本,而不是在每个页面中都定义它。 How do I pass parameters to this script from each page dynamically? 如何动态地从每个页面向该脚本传递参数?
<script type="text/javascript">
var tableId = '#usersChildren\\:userList';
var removeButtonId = '#usersChildren\\:removeUsers';
var headerCheckbox = '#selectAll';
jQuery(document).ready(function() {
if (jQuery(tableId).find("input[type='checkbox']").length == 1) {
jQuery(headerCheckbox).hide();
jQuery(removeButtonId).hide();
} else if(jQuery(tableId).find("input[type=checkbox]:checked").length == 0) {
jQuery(removeButtonId).hide();
jQuery(headerCheckbox).click(function() {
jQuery(tableId).find("input[type='checkbox']").attr('checked', jQuery(headerCheckbox).is(':checked'));
});
}
jQuery(tableId).find("input[type='checkbox']").each(function() {
if(jQuery(this).attr('id') != headerCheckbox) {
jQuery(this).click(function() {
if (jQuery(headerCheckbox).is(':checked')) {
if(jQuery(tableId).find("input[type=checkbox]:checked").length != 1) {
jQuery(removeButtonId).show();
}
} else if(jQuery(tableId).find("input[type=checkbox]:checked").length > 0) {
jQuery(removeButtonId).show();
} else {
jQuery(removeButtonId).hide();
}
});
}
});
});
</script>
Put this code in a script that is sourced from a script tag, on each template that needs it: 将此代码放在需要脚本的每个模板上的脚本标签中:
function bindRemoveButton(tableId, removeButtonId, headerCheckbox) {
if (jQuery(tableId).find("input[type='checkbox']").length == 1) {
jQuery(headerCheckbox).hide();
jQuery(removeButtonId).hide();
} else if(jQuery(tableId).find("input[type=checkbox]:checked").length == 0) {
jQuery(removeButtonId).hide();
jQuery(headerCheckbox).click(function() {
jQuery(tableId).find("input[type='checkbox']").attr('checked', jQuery(headerCheckbox).is(':checked'));
});
}
jQuery(tableId).find("input[type='checkbox']").each(function() {
if(jQuery(this).attr('id') != headerCheckbox) {
jQuery(this).click(function() {
if (jQuery(headerCheckbox).is(':checked')) {
if(jQuery(tableId).find("input[type=checkbox]:checked").length != 1) {
jQuery(removeButtonId).show();
}
} else if(jQuery(tableId).find("input[type=checkbox]:checked").length > 0) {
jQuery(removeButtonId).show();
} else {
jQuery(removeButtonId).hide();
}
});
}
});
}
On each template that needs it do 在每个需要它的模板上
<script type="text/javascript" src="remove.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
var tableId = '#usersChildren\\:userList';
var removeButtonId = '#usersChildren\\:removeUsers';
var headerCheckbox = '#selectAll';
bindRemoveButton(tableId, removeButtonId, headerCheckbox);
});
</script>
Now you can change the bindRemoveButton
function in one place, and in each template you just have to change three variables. 现在,您可以在一个位置更改bindRemoveButton
函数,而在每个模板中,只需更改三个变量。
You can put your script in a php file and pass the variable to the query string. 您可以将脚本放入php文件中,并将变量传递给查询字符串。 Use header("Content-type: application/x-javascript"); 使用标头(“内容类型:应用程序/ x-javascript”); to render it as javascript file. 将其呈现为javascript文件。 Take a look at this example : http://www.webhostingtalk.com/showthread.php?t=494879 看一下这个例子: http : //www.webhostingtalk.com/showthread.php?t=494879
Another way is to check your address using location.href. 另一种方法是使用location.href检查您的地址。 For example your address is page1.html, page2.html page3.html then 例如,您的地址是page1.html,page2.html page3.html,然后
if (location.href.match(/page1/)) { //assign id for page 1 //}
etc... search location.href.match for more example and use 等...搜索location.href.match以获取更多示例和使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.