简体   繁体   English

jQuery colorbox打破了ASP.NET Web窗体中的回发

[英]jQuery colorbox breaks postbacks in ASP.NET Web Forms

We have a web forms project and in it I want to use jQuery's colorbox plugin to pop up a small window with a submit button. 我们有一个Web窗体项目,在其中我想使用jQuery的colorbox插件弹出带有提交按钮的小窗口。 Because we are using web forms, the form tag cannot be part of the colorbox. 因为我们使用的是Web表单,所以form标签不能是colorbox的一部分。 The problem is that when colorbox loads the element in the DOM into the colorbox, it actually moves it to the top of the body into an absolutely positioned element. 问题在于,当颜色盒将DOM中的元素加载到颜色盒中时,实际上会将其移到主体的顶部,成为绝对定位的元素。

Normally this is fine, but it actually takes the contents out of the form tag. 通常,这很好,但是实际上会将内容从form标记中删除。 This makes it so that submit buttons within the colorbox no longer cause post backs. 这样可以使颜色框中的提交按钮不再引起回发。

Here is a fiddle representing the problem: http://jsfiddle.net/Chevex/vbLFD/ 这是代表问题的小提琴: http : //jsfiddle.net/Chevex/vbLFD/

If you click the submit changes button you will notice that the form posts to google and the window loads with google. 如果单击“提交更改”按钮,您会注意到该表单已发布到google,并且该窗口已加载google。 However, if you click the link to load the DIV into colorbox and then click the submit button from within colorbox, nothing happens. 但是,如果您单击链接以将DIV加载到colorbox中,然后在colorbox中单击“提交”按钮,则不会发生任何事情。 The button was taken out of the form tag. 该按钮已从form标签中取出。

Is there an easy fix for this behavior? 是否有解决此问题的简便方法?

Edit 编辑

I thought of submitting the form with jQuery as in this fiddle: http://jsfiddle.net/Chevex/vbLFD/6/ 我想到了用jQuery提交表单,如下所示: http : //jsfiddle.net/Chevex/vbLFD/6/

The problem with that is if the DIV contained other input elements, like text boxes, then they too would be removed from the form tag. 这样做的问题是,如果DIV包含其他输入元素,例如文本框,那么它们也将从表单标签中删除。 So even if the form gets submitted with jQuery, the input values that were supposed to be posted with the form will not be included. 因此,即使表单是使用jQuery提交的,原本应该与表单一起发布的输入值也不会包括在内。

It would seem the only way to fix this would be to have colorbox stay within the form somehow. 似乎解决此问题的唯一方法是使颜色框以某种方式保留在表单中。

We're using Colorbox v1.4.33, and applying the accepted answer did not solve the problem. 我们正在使用Colorbox v1.4.33,应用接受的答案并不能解决问题。

Colorbox creates two separate DIV elements, one with ID=colorbox and one with ID=cboxOverlay. Colorbox将创建两个单独的DIV元素,一个元素的ID = colorbox,另一个元素的ID = cboxOverlay。 You need to move both of these DIV elements in order for the Colorbox dialog to render correctly and the ASP.NET postback to trigger. 您需要移动这两个DIV元素,以便Colorbox对话框能够正确呈现,并触发ASP.NET回发。

    $(function () {
        $("#btnBoligforholdAdd").colorbox({
            inline: true,
            href: "#modalDialog_Boligforhold",
            width: "450px",
            closeButton: false,
            opacity: 0.5,
            onOpen: function () {
                $('#aspnetForm').append($('#cboxOverlay'));
                $('#aspnetForm').append($('#colorbox'));
            }
        });
    });

You can use a simple jQuery block to move it to the top of the main form. 您可以使用一个简单的jQuery块将其移动到主窗体的顶部。

$(document).ready(function() {
  var colorbox = $("#colorbox");
  colorbox.remove(); // Removes from dom
  $('form#idOfForm').prepend(colorbox);
});

Now anything you load in there should be within the global form. 现在,您在其中加载的所有内容都应该全局窗体内

An alternative selector you can use is body > form for the global form, but it's not as fast as an id. 您可以使用的替代选择器是body > form作为全局表单,但它的速度不如id。

The below opens a colorbox of a 2 button form which allowsthe data to be saved back to the page behind function on the Save button. 下方将打开一个2按钮形式的颜色框,该颜色框允许将数据保存回到“保存”按钮上后面的页面。

1) The actual inline form 1)实际的内联表格

<div style="display: none;">
  <div id="formcontent" class="form-horizontal padder">
    <!--no <form> tag here as its inline content-->
  </div>
</div>

2) Javascript 2)Javascript

$(document).ready(function () {    
  $("#colorbox, #cboxOverlay").appendTo('form:first'); //required for colorbox forms!

  $("#<%=cmdAdd.ClientID %>").colorbox({ inline: true, href: "#ColorBoxNewDiagram" });

  $("#cmdNewDiagram").click(function () {
    $.fn.colorbox.close();
  });

  $("#<%=cmdCancelAdd.ClientID%>").click(function () {
    $.fn.colorbox.close();
    return false;
  });
});

Color box appends the hidden container to the < body> tag which is outside the asp < form > tag... 颜色框将隐藏的容器附加到<body>标签,该标签位于asp <form>标签之外...

Solution is: 解决方法是:

Append to < form > tag instead of the < body > tag 附加到<form>标签而不是<body>标签

In the jquery.colorbox.js file, search for the following line: 在jquery.colorbox.js文件中,搜索以下行:

$(document.body).append($overlay, $box.append($wrap, $loadingBay)); $(document.body).append($ overlay,$ box.append($ wrap,$ loadingBay));

replace it with the following line: 将其替换为以下行:

$('form').prepend($overlay, $box.append($wrap, $loadingBay)); $('form')。prepend($ overlay,$ box.append($ wrap,$ loadingBay));

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

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