开发者

Where to put partial specific javascript in rails 2.x

开发者 https://www.devze.com 2022-12-20 22:06 出处:网络
Consider the following partial, it gets inserted into a page if someone clicks AJAX "add comment" link.The question I have is: Is this the best place to include that JavaScript?

Consider the following partial, it gets inserted into a page if someone clicks AJAX "add comment" link. The question I have is: Is this the best place to include that JavaScript?

I thought it was best to put it in the application.js, but because the partial gets added via AJAX, the validate method called within $(document).ready block does not get triggered because the form does not exist when the original document is ready. Does that make sense?

Using Rails 2.3.4 and jQuery 1.4.1

Thanks!

<%form_for [@commentable,@comment] do |f|%>
  <%= f.text_area :comment, :class=>"required", :minlength=>2%>

  开发者_运维百科<% content_tag :button, :type => :submit do %>
    Comment
  <% end %>
<%end%>

<script type="text/javascript">
  $(document).ready(function(){
    $("#new_comment").validate();
  });
</script>


Not sure when you want the validation to occur, but it seems like you could use jQuery's live functionality to attack a validation method defined in your application.js to the incoming partial's submit button.

I was thinking something along the lines of:

$("#new_comment").live('click', function(event) {
  $(this).validate();
  // or maybe event.target.validate(); ?
});
0

精彩评论

暂无评论...
验证码 换一张
取 消