将你的rails应用程序的javascript放入.js.erb文件而不是将其放入application.js文件中有什么好处?我有一个企业的创建按钮,所以我应该将代码放入create.js.erb文件或将其放入我的application.js使用:
$("#business_submit").click(function() {}
Run Code Online (Sandbox Code Playgroud)
除此之外,我有我的创建按钮
$('.error').hide();
$("#business_submit").click(function() {
// validate and process form here
$('.error').hide();
var name = $("input#business_name").val();
if (name == "" || name == "Required Field") {
$('#namelabel').show()
$("#business_name").focus();
return false;
}
var address = $("#business_address").val();
if (address == "" || address == "Required Field") {
$('#addresslabel').show();
$("#business_address").focus();
return false;
}
var city = $("#business_city").val();
if (city == "" || city == "Required Field") {
$('#citylabel').show();
$('#business_city').focus();
return false;
}
var state = $("#business_state").val();
if (state == "" || …Run Code Online (Sandbox Code Playgroud) 我大部分时间都在工作.我的rails链接是:
<%= link_to(image_tag('/images/bin.png', :alt => 'Remove'), @business, :class => 'delete', :confirm => 'Are you sure?', :id => 'trash') %>
Run Code Online (Sandbox Code Playgroud)
:class => "delete"正在调用一个ajax函数,以便删除它并且页面不刷新,效果很好.但由于页面没有刷新,它仍然存在.所以我的id垃圾正在调用这个jquery函数:
$('[id^=trash]').click(function(){
var row = $(this).closest("tr").get(0);
$(row).hide();
return false;
});
Run Code Online (Sandbox Code Playgroud)
这隐藏了我点击的任何垃圾图标的行.这也很有效.我以为我把它全部解决了然后我遇到了这个问题.当你点击我的垃圾箱时,我会弹出这个确认框,询问你是否确定.无论你选择取消还是接受,jquery都会触发并隐藏行.它不会被删除,只有在您刷新页面时才会被隐藏.我尝试更改它以便通过jquery完成提示,但是无论我在提示中选择什么,rails都会删除该行,因为在调用提示时正在调用.destroy函数.
我的问题是如何才能获得取消或接受来自rails确认弹出的值,以便在我的jquery中我可以有一个if语句,如果他们单击"接受"则隐藏,如果单击取消则不执行任何操作.
编辑:回答下面的问题.那没用.我尝试将我的链接更改为:
<%= link_to(image_tag('/images/bin.png', :alt => 'Remove'), @business, :class => "delete", :onclick => "trash") %>
Run Code Online (Sandbox Code Playgroud)
把它放在我的jquery中
function trash(){
if(confirm("Are you sure?")){
var row = $(this).closest("tr").get(0);
$(row).hide();
return false;
} else {
//they clicked no.
}
}
Run Code Online (Sandbox Code Playgroud)
但这个功能从未被调用过.它只是删除它没有提示,并没有隐藏它.但这给了我一个想法.
我接受了ajax正在调用的删除功能
$('a.delete').click (function(){
$.post(this.href, {_method:'delete'}, null, "script");
$(row).hide();
});
Run Code Online (Sandbox Code Playgroud)
并修改它实现您的代码: …
好的.我正在使用模态框弹出窗口来显示动态表的业务详细信息.这张桌很长.一切都适用于模态框,但如果说它们滚动到页面底部,它总是打开页面顶部的模态框.因此他们必须以这种方式进行大量滚动.
我目前正在使用此代码来集中我的模态框.
function centerPopup(x){
//request data for centering
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupHeight = $("#popup" + x).height();
var popupWidth = $("#popup" + x).width();
//centering
$("#popup" + x).css({
"position": "absolute",
"top": windowHeight/2-popupHeight/2,
"left": windowWidth/2-popupWidth/4
});
//only need force for IE6
$('#backgroundPopup').css({
"height": windowHeight
});
}
Run Code Online (Sandbox Code Playgroud)
我不知道这段代码中是否有影响它的东西.一个解决方法是必须向下滚动到它们之前的位置,但我找不到关于.position的文档.