简体   繁体   English

防止在dojo按钮上的ie8中回发

[英]Prevent postback in ie8 on dojo button click

I have the following code in my asp.net page: 我的asp.net页中有以下代码:

<button dojotype="dijit.form.Button">
    <asp:Label ID="lblClear" runat="server" meta:resourcekey="lblClearResource1" />
        <script type="dojo/method" event="onClick" args="evt">
            return ClearCheckBoxes('<%=clientIds.ClientID%>');
        </script>
</button>

Where ClearCheckBoxes is the following: 其中ClearCheckBoxes是以下内容:

function ClearCheckBoxes(obj1) {
    var chks = document.getElementsByTagName("input");
    for (i = 0; i < chks.length; i++) {
        if (chks[i].type == "checkbox") {
            if (chks[i].checked == true) chks[i].checked = false;
        }
    }
    document.getElementById(obj1).value = "";
    document.getElementById('<%=clientsIds.ClientID %>').value = "";

    return false;
}

This code works fine in all browsers, except in IE8, which causes a postback. 该代码在所有浏览器中均能正常工作,但在IE8中除外,这会导致回发。 Is there a way to disable a postback in IE8 in this scenario? 在这种情况下,是否可以禁用IE8中的回发? Everything I've found online said return false; 我在网上找到的所有内容都返回false; should work but that doesn't prevent the postback. 应该可以,但是不会阻止回发。

Typically, the default type for a button is submit , which will end up submitting a surrounding form if one exists. 通常,按钮的默认typesubmit ,如果存在,它将最终提交周围的表单。 If this button is not intended to be submitting the form, adding type="button" to the <button> tag will stop it from behaving like a submit button. 如果此按钮不打算提交表单,则在<button>标记中添加type="button"将阻止其表现为提交按钮。

Reduced example: (try removing type="button" to see the difference in IE8) 简化示例:(尝试删除type="button"以查看IE8中的区别)

<form>
    <button type="button" data-dojo-type="dijit/form/Button">Button
        <script type="dojo/method" event="onClick" args="evt">
            this.set('label', 'Clicked');
        </script>
    </button>
</form>

Take a look at dojo/_base/event#stop ; 看一下dojo/_base/event#stop here's the documentation , emphasis mine: 这是文档 ,重点是我的:

prevents propagation and clobbers the default action of the passed event 防止传播并破坏已传递事件的默认操作

You can include that in your code. 您可以将其包含在您的代码中。

require(['dojo/_base/event']);

<button dojotype="dijit.form.Button">
    <asp:Label ID="lblClear" runat="server" meta:resourcekey="lblClearResource1" />
        <script type="dojo/method" event="onClick" args="evt">
                dojo.stopEvent(evt); // dojo/_base/event extends dojo pre-2.0 
                return ClearCheckBoxes('<%=clientIds.ClientID%>');
        </script>
</button>

dojo/_base/event#stop works cross-browser, including (I believe) in IE8. dojo/_base/event#stop可跨浏览器工作,包括IE8中的(我相信)。

In case it's not feasible to require modules like that for your templates (I've never done anything like that myself), here's the code specific to preventing default behavior in IE: 如果为模板require像这样的模块是不可行的(我自己从未做过类似的事情),这是防止IE中默认行为的特定代码:

evt = evt || window.event;
evt.cancelBubble = true;
on._preventDefault.call(evt);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM