简体   繁体   English

如何以contact7形式定位类并将其添加到textarea?

[英]How do I target and add a class to the textarea in contact7 form?

I am currently working on styling a contact7 form and have run into a slight issue because the jquery snippet I am using(I am a javascript/jquery newbie) only affects inputs and not text areas. 我目前正在设计contact7表单的样式,并且遇到了一个小问题,因为我正在使用的jquery片段(我是javascript / jquery新手)仅影响输入,而不会影响文本区域。 For some reason contact7's text section calls an input yet the textarea doesn't, it only uses textarea. 由于某种原因,contact7的文本部分会调用输入,但textarea不会输入,它仅使用textarea。 Both input and textarea are automatically generated by cf7. 输入和textarea都是由cf7自动生成的。

Right now my text and email fields are working as expected and am trying to figure out how to add and remove classes to the textarea. 现在,我的文本和电子邮件字段正在按预期工作,并且正在尝试弄清楚如何向文本区域添加和删除类。 As mentioned I am a newbie so any guidance would be appreciated. 如前所述,我是新手,所以任何指导将不胜感激。

<script>
    jQuery(function($) {
        $('input').focus(function(){
            $(this).parents('.form-label-outer').addClass('focused');
        });

        $('input').blur(function(){
            var inputValue = $(this).val();
            if ( inputValue == "" ) {
                $(this).removeClass('filled');
                $(this).parents('.form-label-outer').removeClass('focused');  
            } else {
                $(this).addClass('filled');
            }
        })
    });
</script>

<form action="/contact/?preview_id=8&amp;preview_nonce=1ac0cc6b7e&amp;_thumbnail_id=-1&amp;preview=true#wpcf7-f28580-p8-o4" method="post" class="wpcf7-form" novalidate="novalidate">
<div style="display: none;">
<input type="hidden" name="_wpcf7" value="28580">
<input type="hidden" name="_wpcf7_version" value="5.1.1">
<input type="hidden" name="_wpcf7_locale" value="en_US">
<input type="hidden" name="_wpcf7_unit_tag" value="wpcf7-f28580-p8-o4">
<input type="hidden" name="_wpcf7_container_post" value="8">
<input type="hidden" name="g-recaptcha-response" value="">
</div>
<div class="form-group">
      <label class="form-label-outer"><span class="form-label">What is your name?</span> <span class="wpcf7-form-control-wrap your-name"><input type="text" name="your-name" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false"></span> </label> /* Event triggered on input (input automatically generated by cf7) */
    </div>
<div class="form-group">
      <label class="form-label-outer"><span class="form-label">What is your email?</span><span class="wpcf7-form-control-wrap your-email"><input type="email" name="your-email" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-required wpcf7-validates-as-email" aria-required="true" aria-invalid="false"></span></label> /* Event triggered on input (input automatically generated by cf7) */
    </div>
<div class="form-group">
      <span class="wpcf7-form-control-wrap your-message"><textarea name="your-message" cols="40" rows="10" class="wpcf7-form-control wpcf7-textarea" aria-invalid="false"></textarea></span> /* Need to trigger event on textarea (textarea automatically generated by cf7) */
    </div>
<p><input type="submit" value="Send" class="wpcf7-form-control wpcf7-submit"><span class="ajax-loader"></span></p>
<div class="wpcf7-response-output wpcf7-display-none"></div></form>

What I would like to achive is to add and subtract the above mentioned classes to the textarea also. 我想实现的是也将上述类添加和减去到textarea中。

您要查找的选择器是$('input, textarea')

I think you wanted to apply the same thing to the text area as well ie on focus you need to remove form-label-outer class and add focused class similarly you need to do some action on blur as well. 我认为您也想将相同的内容应用于文本区域,即在焦点上,您需要删除form-label-outer类并添加焦点类,您同样需要对模糊进行一些操作。 I would suggest you keep a common class for both input and text area and pass that class in the above function. 我建议您为输入和文本区域保留一个通用类, 在上面的函数中传递该类。

or 要么

You can create an id for the text area alone and you can pass that id to the function. 您可以单独为文本区域创建一个ID,然后可以将该ID传递给函数。

Make sure you have form-label-outer class to the parent of the input or text area otherwise it will not work. 确保您的输入或文本区域的父级具有form-label-outer类,否则它将无法正常工作。

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

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