简体   繁体   English

如何在JavaScript中呈现局部

[英]How to render a partial within javascript

I have two forms on a page which have mostly the same inputs. 我在页面上有两种形式,它们的输入几乎相同。 I have tabs on the screen that switch between each form. 我在屏幕上的选项卡可以在每种形式之间切换。 However when i submit one of the forms, i recieve a 'cant be blank' on the same input fields on the other form. 但是,当我提交其中一种表格时,我在另一种表格的相同输入字段上收到“不能空白”。 I understand why this is happening. 我了解为什么会这样。 I tried to stop this by using a if statement to only render either form depending on which tab is clicked. 我试图通过使用if语句仅根据单击哪个选项卡来呈现两种形式来阻止这种情况。 This did not work as you cant target html id's in an if statement. 这无法正常工作,因为您无法在if语句中定位html id。 Then i tried to use javascript which used a 'show' and 'hide' methods depending on which tab was clicked which also didnt work as i believe that that even though the form is still hidden, it is still actually there. 然后我尝试使用javascript,根据所单击的选项卡,它使用了“显示”和“隐藏”方法,这也没有用,因为我相信即使表单仍处于隐藏状态,它实际上仍然存在。 Now im not sure what to do. 现在我不知道该怎么办。 My only idea is to render the forms within javascript? 我唯一的想法是在javascript中呈现表单?

 <input id="tab1" type="radio" name="tabs" checked>
  <label class="label-header" for="tab1"> UK Address</label>

  <input id="tab2" type="radio" name="tabs">
  <label class="label-header"for="tab2"> International Address </label>

  <!-- <section id="content1"> -->
    <%# if  %>
        <%= f.simple_fields_for :address do |fields| %>
        <%# raise %>
            <section id="content1">
                <%= render "address/fields", fields: fields, addressable: addressable %>
            </section>
        <% end %>
      <%# else %>
        <%= f.simple_fields_for :address do |fields| %>
            <section id="content2">
                <%= render "address/international_fields", fields: fields, addressable: addressable %>
            </section>
        <% end %>


$(document).ready(function() {
  var uk_address = $('#content1');
  var international_address = $('#content2');
  $('#tab1').on('click', function() {
    uk_address.show();
    international_address.hide();
  });

  $('#tab2').on('click', function() {
    uk_address.hide();
    international_address.show();
  });

});

You should be able to enable/disable the items inside the javascript you have with this modification: 通过此修改,您应该能够启用/禁用javascript中的项目:

 $(document).ready(function() {
   var uk_address = $('#content1');
   var international_address = $('#content2');
   $('#tab1').on('click', function() {
     uk_address.removeAttr("disabled").show();
     international_address.attr("disabled", "disabled").hide();
   });

   $('#tab2').on('click', function() {
     uk_address.attr("disabled", "disabled").hide();
     international_address.removeAttr("disabled").show();
   });

 });

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

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