link_to_function и jquery не работают вместе в rails 3.1.0 - PullRequest
2 голосов
/ 15 января 2012

Следующая функция link_to_function работает:

 <%= link_to_function "click", "alert(Hallo world!" %> 

Однако в jquery не работает следующее:

 <%= link_to_function "click", "$('#std').append('<p>my friend!</p>')" %> 

В application.html.erb есть:

<head>
  <title><%= title %></title>
   <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <%= stylesheet_link_tag    "application" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>

JQuery работает хорошо.Есть мысли по поводу проблемы?Спасибо.

Также вот определение API в рельсах 3.1.3 для link_to_function:

link_to_function(name, function, html_options={})

Returns a link whose onclick handler triggers the passed JavaScript.

The helper receives a name, JavaScript code, and an optional hash of HTML options. The name is used as the link text and the JavaScript code goes into the onclick attribute. If html_options has an :onclick, that one is put before function. Once all the JavaScript is set, the helper appends “; return false;”.

The href attribute of the tag is set to “#” unless html_options has one.

link_to_function "Greeting", "alert('Hello world!')", :class => "nav_link"
# => <a class="nav_link" href="#" onclick="alert('Hello world!'); return false;">Greeting</a>

ОБНОВЛЕНИЕ: вот код HTML html:

<a href="#" onclick="$('#std').append('&lt;p&gt;my friend!&lt;/p&gt;'); return false;">test</a> 

Ответы [ 2 ]

7 голосов
/ 18 января 2012

Это навязчивый способ написания JavaScript. Я бы написал что-то вроде этого:

<%= link_to "click", "#", :id=>"click" %> 

Обновление

Вы должны добавить документ, готовый для правильного переплета.

Тогда в вашем приложении. Js

$(function() {
  $("#click").click(function(e){
    $('#std').append('<p>my friend!</p>');
    e.preventDefault();
  });
});
1 голос
/ 18 января 2012

вы тоже используете прототип? если нет, то следующий код должен работать. добавьте это по вашему мнению.

<%= link_to "click", "javascript:", :onclick => "call_function();" %> 

в файле скрипта добавьте следующий код.

function call_function () {
  $('#std').append('<p>my friend!</p>');
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...