[英]JQuery DOM manipulation and event trigger
I am attempting to build a simple edit-in-place feature for a new rails app. 我正在尝试为新的Rails应用程序构建一个简单的就地编辑功能。 To do this I need my changes to save the data. 为此,我需要进行更改以保存数据。
How do I take the div info from the top and enter it in my hidden form :content to save my edit-in-place changes? 如何从顶部获取div信息并以隐藏形式:content输入它以保存在位编辑更改?
My current show.html page is as follows 我当前的show.html页面如下
<p id="notice"><%= notice %></p>
<div id="body_editable" class="editable">
<%= @article.content %>
</div>
<%= form_for(@article) do |f| %>
<%= f.label :content %><br>
<%= f.hidden_field :content %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<script>
var editor = new MediumEditor('.editable');
</script>
I am assuming like the following are needed: 我假设需要以下内容:
//on page load to bring the data to the div
$('#body_editable').html($('#article_content').val());
//on save to take the div and put it in the form for :content
$('#article_content').val(editor.serialize().body_editable.value);
but I am not sure how to trigger the save event using a rails submit button 但我不确定如何使用Rails提交按钮触发保存事件
try: 尝试:
$('select your submit').click(function(e){
e.preventDefault();
$('#article_content').val(editor.serialize().body_editable.value);
$(this).closest('form').submit();
});
While Alex's answer will work, it's safer to do: 尽管亚历克斯的答案行之有效,但这样做更安全:
$('form[for=article]').submit(function(e){
e.preventDefault();
$('hidden-field').val($('#body_editable').text());
$(this).submit();
});
This way it gets the specific elements and avoids memory leaks by using functions that return the value rather than getting the native value, which allocates a new address in memory when you set things to someElement.value
. 这样,它通过使用返回值的函数而不是获取本机值的函数来获取特定元素并避免内存泄漏,当您将内容设置为someElement.value
时,本机值将在内存中分配一个新地址。
Capture the form's submit event, copy the value over, then submit(). 捕获表单的submit事件,将值复制过来,然后submit()。
Something like this: 像这样:
$('form[id^="edit_article"]').on('submit', function(e) {
e.preventDefault();
$('#article_content').val(editor.serialize().body_editable.value);
$(this).submit()
return false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.