简体   繁体   English

如何将值从 JavaScript 发送到表单并获取该值作为表单 ID

[英]How to sent a value from JavaScript to a form and get the value as form id

I have a fullcalendar script.我有一个完整的日历脚本。 I have already created a event for today (for eg), now I want to edit it, so on click on today's date I have to send a value from a JavaScript to my form to edit this event.我已经为今天创建了一个事件(例如),现在我想编辑它,所以点击今天的日期我必须从 JavaScript 发送一个值到我的表单来编辑这个事件。

This is my onclick JavaScript function这是我的 onclick JavaScript 函数

var initialize_calendar;
initialize_calendar = function() {
  $('.calendar').each(function(){
      eventClick: function(event, jsEvent, view) {
        $.getScript(event.edit_url, function() {
          $('#event_id').val(event.id);
        });
      }
    });
  })
};
$(document).on('turbolinks:load', initialize_calendar);

The value from $('#event_id').val(event.id);来自$('#event_id').val(event.id); I'm getting in my form as 32. You can see it in form input field below form我在我的表单中输入 32。您可以在表单下方的表单输入字段中看到它

<form class="simple_form edit_event" id="edit_event_#{#event_id}" action="/securities/events/#{#event_id}" accept-charset="UTF-8" data-remote="true" method="post"><input name="utf8" type="hidden" value="✓">
<input type="hidden" name="_method" value="patch">
        <input type="hidden" value="32" name="event[id]" id="event_id">
</form>

My problem is我的问题是

The same event id 32, I want to append in my form id as id="edit_event_32" and in form action action="/securities/events/32" .相同的事件 id 32,我想在我的表单 id 中附加id="edit_event_32"和表单 action action="/securities/events/32"

You can use the jQuery parent() method to get the parent of an element.您可以使用 jQuery parent()方法来获取元素的父级。 After getting the parent, you can just set the id attribute using the jQuery attr() method.获得父级后,您可以使用 jQuery attr()方法设置id属性。 The code would be as below.代码如下。

$('#event_id').parent().attr('id','edit_event_'+event.id);

Now, to set the form action, you can still use the same technique.现在,要设置form操作,您仍然可以使用相同的技术。 The code would be as below.代码如下。

$('#event_id').parent().attr('action','/securities/events/'+event.id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM