简体   繁体   English

如何动态地从每个页面将参数传递给javascript

[英]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.

相关问题 如何将参数从 Javascript 传递到新页面? - How can I pass parameters to a new page from Javascript? 如何将Django的字符串参数传递给javascript函数? - How do I pass string parameters from django to javascript function? 如何在Javascript + Node中传递参数? - How do I pass parameters in Javascript + Node? 如何将控制器和动作参数传递给 Rails 3 中的 javascript 函数? - How do I pass the controller and action parameters to a javascript function in Rails 3? 如何将数据库列值作为JavaScript函数的参数传递? - How do I pass database column values as parameters of a JavaScript function? 如何通过sprintf()将PHP参数传递给javascript函数? - How do I pass PHP parameters through an sprintf() to a javascript function? 如何仅使用JavaScript将表单数据从一个HTML页面传递到另一HTML页面? - How do I pass form data from one HTML page to another HTML page using ONLY javascript? 如何将第1页的输入字段传递给第2页的隐藏字段,然后在javascript中显示第2页到第3页的隐藏字段? - How do I pass input fields from page 1 to hidden fields on page 2 and then display hidden fields from page 2 to page 3 in javascript? 如何从参数动态构建JavaScript指令? - How can I build JavaScript instruction dynamically from parameters? 如何检测 web 页面是否从 Python 中的 Javascript 动态呈现? - How do I detect if a web page is rendered dynamically from Javascript in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM