javascript - 延迟检查其他复选框更改的多个复选框选择

标签 javascript jquery jquery-mobile asp-classic

我被迫使用 jQuery Mobile,尽管它是一个构建良好的系统,但我还是有些犹豫。因此,我有一个在任何复选框更改时提交的表单。客户希望这样,当他们单击一个页面时,页面会等待一段时间提交,这样,如果选择另一个页面,则不会触发前一个请求,而会触发新请求。它本质上是为了减少即时性并允许更有效的过滤/请求。我的代码是这样的:

$( function() {

    var sForm = "form[name='Waves']",
        sCboxes = "input[type='checkbox']",
        sWaves = "";

    var $Cboxes = $(sForm + " " + sCboxes),
        sChecked = sCboxes + ":checked";

    $Cboxes.change( function(event) {

        var $this = $(this);

        if ( $this.attr("id") != "ClearAll" ) {

            console.debug($(this));

            console.debug('Changing page.');

            sWaves = "";

            // Form the string for the selected waves with standard check-if-it's-the-last
            // element logic for comma generation.

            $.each( $(sChecked) , function(i, v) {

                var $this = $(this);

                var iIndex = v.value;

                sWaves += iIndex + ( ( i == $(sChecked).length - 1 ) ? "" : "," );

            } );

            console.debug("Waves to select: " + sWaves);

            $.mobile.changePage("default.asp", {

                data: { Wave: sWaves },     // Submit the previously formed string

                type: "post"

            } );

            //$(sForm).submit();

        } else {

            $(sChecked).add( $(this) ).attr("checked", false).checkboxradio("refresh");

            $.mobile.changePage("default.asp", {

                data: { Wave: "" },

                type: "post"

            } );

        }

    } );

    $("#ClearAll").click( function(event) {

        event.preventDefault();

    } );

    $(".slideout").click( function(){

        $(this).parents(".ui-collapsible").find(".ui-icon-minus").click();

    } );

} );

表单的 HTML:

<form ACTION="<%=ASP_SELF()%>" ID="Waves" METHOD="post" NAME="Waves">

<% ' Put the scripts we need here so that it loads with the page. %>
<script SRC="/base/scripts/scripts.js"></script>

<fieldset DATA-ROLE="controlgroup" DATA-TYPE="horizontal" STYLE="margin-left:5px; margin-bottom:0px; ">
       <input TYPE="checkbox" NAME="#" ID="#" VALUE="Select Waves" CLASS="custom"   />
       <label CLASS="no-hover" FOR="#">&nbsp;Waves:&nbsp;</label>
       <input TYPE="checkbox" NAME="Wave1" ID="Wave1" VALUE="1" CLASS="custom" STYLE="width:70px !important; " <% If Instr(request("Wave"),"1") OR WaveOne = "YES" Then response.write " checked=""checked""" End If %> />
       <label FOR="Wave1">&nbsp;1&nbsp;</label>
       <input TYPE="checkbox" NAME="Wave2" ID="Wave2" VALUE="2" CLASS="custom" STYLE="width:70px !important; " <% If Instr(request("Wave"),"2") OR WaveTwo = "YES" Then response.write " checked=""checked""" End If %> />
       <label FOR="Wave2">&nbsp;2&nbsp;</label>
       <input TYPE="checkbox" NAME="Wave3" ID="Wave3" VALUE="3" CLASS="custom" STYLE="width:70px !important; "<% If Instr(request("Wave"),"3") OR WaveThree = "YES" Then response.write " checked=""checked""" End If %> />
       <label FOR="Wave3">&nbsp;3&nbsp;</label>
       <input TYPE="checkbox" NAME="Wave4" ID="Wave4" VALUE="4" CLASS="custom" STYLE="width:70px !important; "<% If Instr(request("Wave"),"4") OR WaveFour = "YES" Then response.write " checked=""checked""" End If %> />
       <label FOR="Wave4">&nbsp;4&nbsp;</label>

       <input TYPE="checkbox" NAME="ClearAll" ID="ClearAll" VALUE="Clear" CLASS="custom" $('input[data-type="search"]').TRIGGER("CHANGE").VAL(""); />
       <label FOR="ClearAll">&nbsp;Clear&nbsp;&nbsp;</label>
</fieldset>
    </form>

我需要延迟 $.mobile.changePage 调用足够长的时间以允许其他相关的复选框(在同一字段集中)也被切换。我感谢任何输入!这是一件非常重要的事情。

最佳答案

var timer;
var waitTime = 2000;
$Cboxes.change( function(event) {

 if(timer)clearTimeout(timer);

 timer = setTimeout(function(){
  //your logic
  },waitTime);

 });

当触发更改事件时,要执行的操作计划在 2000 毫秒后执行, 如果用户在此之前改变主意,只需清除间隔并创建一个新间隔即可

关于javascript - 延迟检查其他复选框更改的多个复选框选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11551266/

相关文章:

javascript - jQuery - 名称属性中的方括号出错

javascript - 在网站上显示 GitHub 提交

javascript - Jquery/Javascript - 变量正在获取另一个变量值

jquery - 如何创建类似于 Google+ 中的工具提示?

css - jQuery Mobile 不在动态复选框上应用样式并且无法单击

javascript - 无法在 'replaceState' 上执行 'History' <local_URL> 无法在源为 'null' 的文档中创建

javascript - Vista/W7 小工具的 javascript 中的字符串操作

javascript - 如何在javascript中更改滚动条的背景颜色

jquery - 未满足的对等依赖 popper.js

javascript - 在 jQuery Mobile 中动态构建可折叠数据集的技术是什么