Rails和JQuery-can';t禁用链接默认操作
我有一个“checked”链接,它指向相同名称的控制器操作。其路由为POST请求。我已经设置了代码,但是发生的是jquery$。post会按需要触发,但html请求也会发生,这意味着我的操作被调用了两次。我不明白为什么。有人能认出我犯的错误吗 在my application.js中Rails和JQuery-can';t禁用链接默认操作,jquery,ruby-on-rails,Jquery,Ruby On Rails,我有一个“checked”链接,它指向相同名称的控制器操作。其路由为POST请求。我已经设置了代码,但是发生的是jquery$。post会按需要触发,但html请求也会发生,这意味着我的操作被调用了两次。我不明白为什么。有人能认出我犯的错误吗 在my application.js中 jQuery.ajaxSetup({ 'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} }); $(
jQuery.ajaxSetup({
'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
});
$(document).ready(function() {
$('ul li.sentence_summary a.correct').click(function(){
$(this).css("border", "1px black solid");
$.post($(this).attr("href"), null, null, "script");
return false;
});
});
在我的控制器中
def check
@sentence_author = User.find(params[:user_id])
@sentence = @sentence_author.sentences.find(params[:id])
@sentence.checked_at = Time.now
@sentence.checker_id = current_user.id
respond_to do |format|
if @sentence.save!
flash[:notice] = "Thank you for checking #{@sentence_author.to_s}s sentence."
format.js { }
format.html { redirect_to user_foreign_sentences_path(current_user, :display => :pending) }
format.xml { head :ok }
end
end
end
在actions js文件中
alert("testing123");
更新
我的观点是,一组句子都带有这一部分。分部调用下面所示的视图辅助方法来呈现链接
%li{:id => ["sentence", "#{sentence.id}"], :class => "sentence_summary bottom_border_light"}
%div.left
= thumbnail_link_to_user_profile(sentence.user, User::GRAVATAR_M)
%div.left.main_info_container
%div
= link_to_user_profile(sentence.user)
= t('sentences.table.wrote')
%div.main_focus
= sentence.text
%div.bottom_bar
%div.left
= created_at_linked_to_sentence_show_path(sentence)
%div.right.rollover_options
= render_check_options(sentence)
= render_edit_options(sentence)
= render_flag(sentence)
%div.clear
%div.clear
这个助手呈现有问题的链接
def render_check_options( sentence)
if sentence.user != current_user and sentence.language == current_user.native_language and sentence.pending?
html = link_to("correct", check_user_sentence_path(sentence.user, sentence), :method => "post", :class => "action_options underline_on_hover always_blue correct")
html += link_to("incorrect", new_sentence_correction_path(sentence), :class => "action_options underline_on_hover always_blue incorrect")
return html
end
end
更新2*
根据Tony的建议,以下是我的firebug控制台中发生的情况
console.debug($('ul li.sentence_summary a.correct'));
按预期输出正确的链接(我单击这些链接,它们会转到正确的html)
但即使在运行以下两个命令后,链接也不会被禁用
$('ul li.sentence_summary a.correct').unbind('click');
$('ul li.sentence_summary a.correct').click(function(){
return false;
});
对于ul中的第一个li,我的html看起来是这样的(请注意,除非滚动,否则相关链接是隐藏的,因此whey there display设置为:hidden here)
写的
这是一个很好的解决方案。
尝试删除
$(this).css("border", "1px black solid");
$.post($(this).attr("href"), null, null, "script");
返回false并告诉我发生了什么
更新
如果将链接的onclick设置为returnfalse,那么它应该不会返回任何内容。我会检查两件事-
1) 确保选择器正确:
执行此操作时的输出是什么
console.debug($('ul li.sentence_summary a.correct'));
2) 确保没有其他单击处理程序干扰:
选中#1后,打开JS控制台并执行以下操作:
$('ul li.sentence_summary a.correct').unbind('click');
这将解除所有单击事件(包括您希望发生的事件)的绑定。然后通过在控制台中执行通常的代码重新绑定click事件(在click处理程序中仅返回false)。这将确保只有一个处理程序侦听链接的单击事件,并且一个处理程序将返回false而不执行链接
3) 发布有问题的生成HTML,以便我们可以查看
html = link_to("correct", check_user_sentence_path(sentence.user, sentence), :method => "post", :class => "action_options underline_on_hover always_blue correct")
:method=>“post”
可能是您在这里遇到问题的根源。这告诉Rails构建一个提交表单,这意味着您的返回false代码>没有做您认为它正在做的事情。我会尝试以下方法:
$('ul li.sentence_summary a.correct').click(function(e){
e.preventDefault();
$(this).css("border", "1px black solid");
$.post($(this).attr("href"), null, null, "script");
});
请发布构建锚定标签的视图代码。嗨,托尼,谢谢你的回复。我照你说的做了,但它仍在提出要求,并呼吁采取行动。托尼谢谢你这么做,只是想让你知道,我这两天一直在换工作,所以不能按照你的建议去做。我会试试你说的,明天发布html。注意这个空间!非常感谢!!托尼尝试了你的建议,但没有成功。我用控制台输出更新了这个问题。我使用link\u to和:method=>:post在rails中生成链接。正如jdl提到的,这可能是我的问题的根源吗?
html = link_to("correct", check_user_sentence_path(sentence.user, sentence), :method => "post", :class => "action_options underline_on_hover always_blue correct")
$('ul li.sentence_summary a.correct').click(function(e){
e.preventDefault();
$(this).css("border", "1px black solid");
$.post($(this).attr("href"), null, null, "script");
});