【问题标题】:Create a global jQuery function to be used by multiple controls创建一个供多个控件使用的全局 jQuery 函数
【发布时间】:2014-02-18 15:05:05
【问题描述】:

我有多个控件,不同类型的控件具有相同的行为。 如何使用单个函数来重用相同的行为。现在我对他们每个人都有一个功能。这个函数可以正常工作,但我想找到一种方法,如果我们可以用一个全局函数替换这个函数并重用它。

$(document).ready(function () {

    var threechecked = false;
    var fourchecked = false;

    $('#<%=CheckBoxList1.ClientID%> input:checkbox').click(function () {

        var currentIdone = 'Unknown';
        var currentId = $(this).next().html();
        if (currentId == currentIdone) {

            if (threechecked) {

                $("#<%=CheckBoxList1.ClientID%> input").removeAttr('disabled');
                threechecked = false;
                return;
            }
            else {
                $("#<%=CheckBoxList1.ClientID%> input").attr('checked', false);
                $(this).attr('checked', true);
                $('#<%=CheckBoxList1.ClientID%> input:not(:checked)').attr('disabled', 'disabled');
                threechecked = true;
            }


        }

    });


    $('#<%=CheckBoxList2.ClientID%> input:checkbox').click(function () {


        var currentIdone = 'Unknown';
        var currentId = $(this).next().html();
        if (currentId == currentIdone) {

            if (fourchecked) {

                $("#<%=CheckBoxList2.ClientID%> input").removeAttr('disabled');
                checked = false;
                return;
            }
            else {
                $("#<%=CheckBoxList2.ClientID%> input").attr('checked', false);
                $(this).attr('checked', true);
                $('#<%=CheckBoxList2.ClientID%> input:not(:checked)').attr('disabled', 'disabled');
                fourchecked = true;
            }


        }

});

有没有一种方法可以创建单个函数并由每个控件重用相同的函数?

【问题讨论】:

标签: jquery jquery-ui validation checkboxlist


【解决方案1】:

有些人可能会争辩说,将函数附加到全局 window 对象可能有点奇怪,但实际上它与全局函数相同。

window.foo = function() {
    /* Your code here */
};

然后你可以在 DOM 准备好时执行这个函数:

$(document).ready(window.foo);

【讨论】:

    【解决方案2】:

    我为您的问题做了一个解决方案示例。如果你愿意,我把它放在JSBin

    假设你有这个 HTML:

    <div class="unknownSelectable">
      <input type="checkbox" value="0" name="Anything" id="Anything1" />
      <label for="Anything1">Unknown</label>
      <input type="checkbox" value="0" name="Anything" id="Anything2" />
      <label for="Anything2">Label 1</label>
      <input type="checkbox" value="0" name="Anything" id="Anything3" />
      <label for="Anything3">Label 2</label>
      <input type="checkbox" value="0" name="Anything" id="Anything4" />
      <label for="Anything4">Label 3</label>
    </div>
    <h1>Select</h1>
    <div class="unknownSelectable">
      <input type="checkbox" value="0" name="AnotherAnything" id="AnotherAnything1" />
      <label for="AnotherAnything1">Unknown</label>
      <input type="checkbox" value="0" name="AnotherAnything" id="AnotherAnything2" />
      <label for="AnotherAnything2">Label 1</label>
      <input type="checkbox" value="0" name="AnotherAnything" id="AnotherAnything3" />
      <label for="AnotherAnything3">Label 2</label>
      <input type="checkbox" value="0" name="AnotherAnything" id="AnotherAnything4" />
      <label for="AnotherAnything4">Label 3</label>    
    </div>
    

    只要放这个 Javascript 就可以了:

    $( ".unknownSelectable input" ).change(function( event ){
      
      var labelSelected = $( this ).next().html();
      
      if ( labelSelected == "Unknown" ) {
        var $siblings = $( this ).siblings( "input[type='checkbox']" );
        
        if( $( this ).is( ":checked" ) )
          $siblings.prop( "checked", false );
        else
          $siblings.prop( "checked", true );
        
      }
      
    });
    

    我将事件从单击更改为更改,因为单击输入或标签都没有关系。我也将它包裹在一个 div 元素中,但无论如何,您也可以将它包裹在另一个 HTML 元素中。

    对不起我的英文,如果我有一些英文错误,请随时重新编写和改进。

    【讨论】:

    • 你能在这里显示渲染的 HTML 吗?不是 ASPX 代码,而是呈现的 HTML。
    • 我不确定你的意思,这是一个 aspx 控件,我必须实现为 aspx 页面。我还看到一件事,它在我的 ASPX 页面中不起作用,而在 HTML 页面中起作用。这不是一个 html 页面,每次加载页面时它都会给我一个脚本错误,说该对象不支持此属性或方法。 jquery02.0.3.min.js。它仅适用于 HTML5。我正在使用 SharePoint 2010 Visual Webpart 来实现这一点。
    • 我明白了,我在 aspx 和 JQuery 方面经历了很多。您的 CheckBoxLists 是否有一些隐藏事件(代码隐藏事件)?
    • 我有,但我不能使用回发,所以 checkboxlist 后面没有事件
    • 您在 Chrome 或 Firefox 控制台中见过吗?如果是这样,会出现什么错误?
    【解决方案3】:

    我猜它是这样工作的

    $( ".unknownSelectable input" ).change(function( event ){
    
      var labelSelected = $( this ).next().html();
    
      if ( labelSelected == "Unknown" ) {
        var $siblings = $( this ).siblings( "input[type='checkbox']" );
    
        if( $( this ).is( ":checked" ) )
        {
          $siblings.prop( "checked", false );
          $siblings.attr( "disabled", "disabled" );    
        }
        else{
          $siblings.removeAttr( "disabled" );
        }
    
      }
    
    });
    

    JSBin

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 2018-09-27
      • 2014-09-26
      • 1970-01-01
      相关资源
      最近更新 更多