简体   繁体   English

选择局部视图的特定元素

[英]Select specific element of a partial view

I'm having difficulty applying a javascript function inside a partial view. 我在局部视图中应用javascript函数有困难。

My goal here is to verify that email was typed correctly and insert a message text depending on the outcome of the condition. 我的目标是验证电子邮件是否正确键入,并根据条件的结果插入消息文本。

This email field is inside a partial that is loaded. 此电子邮件字段位于已加载的部分文档中。 This partial is loaded if the user has one or more emails, thus loading as many partials as needed. 如果用户有一个或多个电子邮件,则会加载此部分,从而根据需要加载尽可能多的部分。

My problem here is that for every partial that my page loads, the function replicates, I'd like the partial validation result not to interfere with the other. 我的问题是,对于页面加载的每个部分,该函数都会复制,我希望部分验证结果不会干扰其他部分。 Follow the partial code below. 请遵循下面的部分代码。

<li class="control-group3 nested-fields">
  <div class="form-group">
    <label class="col-sm-2 control-label">Email:</label>
    <div class="col-sm-5">
      <%= person.text_field :name, class:"form-control" %>
    </div>
  </div>
  <br/>
  <div class="form-group">
    <label class="col-sm-2 control-label">Email Type:</label>
    <div class="col-sm-5">
      <%= person.collection_select :email_type_id, @email_type,
      :id, :name, {prompt: true}, {class: "form-control m-b"} %>
    </div>
  </div>
  <br/>
  <%= link_to_remove_association button_tag('Remover', type: "button", class: "btn btn-link"), person %>
  <hr/>
</li>
<script>

**$("input[name*='[emails_attributes]']").blur(function() {**
  var input = $(this).val();
  function IsEmail(email) {
    var exclude = /[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
    var check = /@[\w\-]+\./;
    var checkend = /\.[a-zA-Z]{2,3}$/;
    if (((email.search(exclude) != -1) || (email.search(check)) == -1) || (email.search(checkend) == -1)) {
      return false;
    } else {
      return true;
    }
  }

  **if (IsEmail(input)) {
    $(this).after("<div id='msgemail'>E-mail válido</div>");
  } else {
    $(this).after("<font color='red'>E-mail inválido </font>");
  }**
});

</script>

First of all, you need to separate the javascript logic from this partial (to avoid logic repetition). 首先,您需要将JavaScript逻辑与此部分分开(以避免逻辑重复)。

And I suggest a different approach: 我建议采用另一种方法:

https://jsfiddle.net/vinceshere/np3v2qnf/28/ https://jsfiddle.net/vinceshere/np3v2qnf/28/

$(".email-validation").on("input", function() {
  var email = $(this).val();

  if (IsEmail(email)) {
    showSuccess($(this));
  } else {
    showError($(this));
  }
});

$("form").on("submit", function(event) {
    event.preventDefault();
  var email = $(this).find('.email-validation').val();

  if (IsEmail(email)) {
    showSuccess($(this));
  } else {
    showError($(this));
  }
});

function showError(element) {
    element.parents('form').find('.success').hide();
  element.parents('form').find('.error').show();
}

function showSuccess(element) {
    element.parents('form').find('.success').show();
  element.parents('form').find('.error').hide();
}

function IsEmail(email) {
  var exclude = /[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
  var check = /@[\w\-]+\./;
  var checkend = /\.[a-zA-Z]{2,3}$/;
  if (((email.search(exclude) != -1) || (email.search(check)) == -1) || (email.search(checkend) == -1)) {
    return false;
  } else {
    return true;
  }
}

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

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