当我通过远程调用或jQuery远程加载局部/内容时,jQuery ujs无法正常工作 [英] jquery ujs not functioning when I remotely load partials/content with remote calls or jQuery

查看:65
本文介绍了当我通过远程调用或jQuery远程加载局部/内容时,jQuery ujs无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段jQuery代码,它用一些内容填充了页面上的元素:

I have a piece of jQuery code that fills an element on the page with some content:

 var content = $("#note_"+note.id).html();
 $("another_div").html(content);

这代替了另一个div的html,但是问题是任何数据远程属性都不再对"another_div"内的任何内容起作用.我认为问题在于jquery_ujs对于页面加载时页面上的项目非常有效,但是如果我远程加载内容,则会失去该功能.

This replaces the html of the other div fine, but the problem is that any data-remote attributes no longer function on any content inside "another_div". I think the problem is that jquery_ujs is working great for the items on the page at pageload, but if I remotely load content, I lose that functionality.

我该如何解决?我也对此感兴趣,因为它对远程加载的局部变量有影响

How do I work around this? I am interested in this as well for its ramifications of remotely loaded partials

推荐答案

我以前曾经遇到过,问题是jQuery已经在加载时解析了所有DOM元素,因此当DOM已更改.您需要手动调用another_div的验证解析.您可以使用以下方法做到这一点:

I've experienced this before, the problem is that jQuery has already parsed all of the DOM elements on load, therefore won't be called again automatically when the DOM is changed. You need to manually recall validation parse for your another_div. You can do this by using the following:

$.validator.unobtrusive.parse("#another_div")

有点技巧,但是我们可能需要将jQuery踢到屁股

var $ main = $(顶级标签");

var $main= $("top level tag");

// Unbind validation
$main.unbind();
$main.data("validator", null);

// Reparse document
$.validator.unobtrusive.parse(document);

//Re add to Main
$main.validate($main.data("unobtrusiveValidation").options);

这篇关于当我通过远程调用或jQuery远程加载局部/内容时,jQuery ujs无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆