当字段为空时,在submit表单中添加类。

时间:2022-11-28 15:10:31

I have a form that has several fields all within the class Contact that have a common header.

我有一个表单,它有多个字段,它们都在类Contact中,它们有一个共同的标头。

I need that header to turn red whenever the user hits submit with any contact field unfilled. These fields (first_name, last_name, company, Marketing_Phone, email) already have the class error added when submitted empty.

我需要这个头在用户点击提交时出现红色。这些字段(first_name、last_name、company、Marketing_Phone、email)在提交空时已经添加了类错误。

These fields are all in the class wrap_contact with the label Contact Information *. This label does not receive the class error on submit. This is what I need to add. I gave the label an id of label2. So my if then is if first_name, last_name, company, Marketing_phone, or email is null on submit then label2 has the error class added.

这些字段都在类wrap_contact中,带有标签联系信息*。此标签不接收提交的类错误。这是我需要添加的。我给标签贴上标签。所以我的if then是如果first_name, last_name, company, Marketing_phone,或者email是null,那么label2就添加了错误类。

I attempted adding the following code, but it did not work.

我尝试添加以下代码,但它不起作用。

 $( "#pardot-form" ).submit(function( event ) {
    var someElements = document.querySelector("p.error,input[name=263882_11763pi_263882_11763]}");

    if (someElements !== "") {
       document.getElementById('p.label2').addClass = 'error';
    }

 };

This form is visible online at http://link.rubiconglobal.com/l/263882/2017-03-24/7ssj.

该表单在http://link.rubiconglobal.com/l/263882/2017-03-24/7ssj中可见。

2 个解决方案

#1


0  

The problem is that your submit still occurs and the page is reloaded. You will need to add event.preventDefault() and event.stopPropagation() to prevent submit action from occurring and reloading the page. Alternatively, since you are using jQuery, you can simply return false which does both. See: https://*.com/a/1357151/1736092

问题是,您的提交仍然发生,页面重新加载。您需要添加event.preventDefault()和event.stopPropagation(),以防止提交操作发生并重新加载页面。或者,由于您使用的是jQuery,您可以简单地返回false,它同时执行这两个操作。参见:https://*.com/a/1357151/1736092

#2


0  

If x element has a class then add class to y element:

如果x元素有一个类,那么将类添加到y元素:

     <script>
     if( $('.first_name, .last_name, .company, .zip, .Marketing_Phone, .email').hasClass('error') === true ) 
    {
    $('p#label2').addClass('label2error');
    }
    </script>

#1


0  

The problem is that your submit still occurs and the page is reloaded. You will need to add event.preventDefault() and event.stopPropagation() to prevent submit action from occurring and reloading the page. Alternatively, since you are using jQuery, you can simply return false which does both. See: https://*.com/a/1357151/1736092

问题是,您的提交仍然发生,页面重新加载。您需要添加event.preventDefault()和event.stopPropagation(),以防止提交操作发生并重新加载页面。或者,由于您使用的是jQuery,您可以简单地返回false,它同时执行这两个操作。参见:https://*.com/a/1357151/1736092

#2


0  

If x element has a class then add class to y element:

如果x元素有一个类,那么将类添加到y元素:

     <script>
     if( $('.first_name, .last_name, .company, .zip, .Marketing_Phone, .email').hasClass('error') === true ) 
    {
    $('p#label2').addClass('label2error');
    }
    </script>