I have a rails app, if the user is not logged in, I am redirecting to a page, which has one br tag with a class. Like this
我有一个rails应用程序,如果用户没有登录,我将重定向到一个页面,其中有一个带有类的br标签。像这样
<br class="logged">
In the Javascript on ready of that function, I am triggering a modal as follows.
在准备好该函数的Javascript中,我按如下方式触发模态。
$(document).ready(function(){
$('.logged').ready(function(){
$('#open-login').click();
});
});
This is working fine, except this modal is getting triggered on every page of the app. I mean that br tag is there in only page of the app, how it is ready for every page is what I don't understand. If anyone can tell what went wrong with my approach, it would be of great help.
这工作正常,除了这个模态在应用程序的每个页面上被触发。我的意思是br标签只存在于应用程序的页面中,如何为每个页面做好准备是我不明白的。如果有人能说出我的方法出了什么问题,那将会有很大的帮助。
ps: It's rails application
ps:这是rails应用程序
2 个解决方案
#1
1
You can try this:
你可以试试这个:
$(document).ready(function(){
if ($('.logged').length > 0)
$('#open-login').click();
}
});
Into if condition you can declare an element of specific page and in only that page you can execute an action.
在if条件中,您可以声明特定页面的元素,并且只在该页面中您可以执行操作。
#2
1
The jQuery .ready()
method can only be called on a jQuery object matching the current document. Attaching it to a $('.logged')
selector still makes its handler function get called when the document is ready - it doesn't care about the selector.
jQuery .ready()方法只能在与当前文档匹配的jQuery对象上调用。将它附加到$('。logged')选择器仍然会在文档准备好时调用其处理函数 - 它不关心选择器。
MarcoSantino's answer will work for your needs, although you may find it cleaner to add the logged-in
class to the body tag instead of inserting a new br tag, and then use the following in your JavaScript:
MarcoSantino的答案将满足您的需求,尽管您可能会发现将登录类添加到body标签而不是插入新的br标签更清晰,然后在JavaScript中使用以下内容:
$(document).ready(function(){
if ($(body).hasClass('logged-in')) {
$('#open-login').click();
}
})
#1
1
You can try this:
你可以试试这个:
$(document).ready(function(){
if ($('.logged').length > 0)
$('#open-login').click();
}
});
Into if condition you can declare an element of specific page and in only that page you can execute an action.
在if条件中,您可以声明特定页面的元素,并且只在该页面中您可以执行操作。
#2
1
The jQuery .ready()
method can only be called on a jQuery object matching the current document. Attaching it to a $('.logged')
selector still makes its handler function get called when the document is ready - it doesn't care about the selector.
jQuery .ready()方法只能在与当前文档匹配的jQuery对象上调用。将它附加到$('。logged')选择器仍然会在文档准备好时调用其处理函数 - 它不关心选择器。
MarcoSantino's answer will work for your needs, although you may find it cleaner to add the logged-in
class to the body tag instead of inserting a new br tag, and then use the following in your JavaScript:
MarcoSantino的答案将满足您的需求,尽管您可能会发现将登录类添加到body标签而不是插入新的br标签更清晰,然后在JavaScript中使用以下内容:
$(document).ready(function(){
if ($(body).hasClass('logged-in')) {
$('#open-login').click();
}
})