简体   繁体   English

JS:在输入更改时显示textarea字段

[英]JS: show textarea field on input change

I've a form that should show a textarea when usr uploads a file. 我有一个表格,当usr上传文件时,该表格应显示文本区域。

I need to show a textarea field if user uploads an imagen, in an image field. 如果用户在图像字段中上传图像,我需要显示一个textarea字段。 However, actually my code does not do that, even provided that the javascript is inside the html. 但是,实际上,即使javascript位于html内,我的代码也不会这样做。

Here is the CODEPEN for this: 这是为此的CODEPEN:

https://codepen.io/ogonzales/pen/GPZBjL https://codepen.io/ogonzales/pen/GPZBjL

I've this JS, but does not recognize when user uploads a file, what should I change on my code? 我有这个JS,但是当用户上传文件时无法识别,我应该对我的代码进行哪些更改?

<script>
            $(function () {
                $("input:file").change(function () {
                    var fileName = $(this).val();
                    if (fileName != "") {
                        $("#instrucciones-adicionales").show();
                    } else {
                        $("#instrucciones-adicionales").hide();
                    }
                });
            });
        </script>

html: HTML:

<div class="row padding80-top">

  <div class="col-md-3 mb-4"></div>
  <div class="col-md-6 mb-4">
     <p class="text-size60 bold-font">Sube tu imagen</p>
     <form method="POST" enctype="multipart/form-data" action="">
        <input type="hidden" name="csrfmiddlewaretoken" value="feLAxC4KQTvAL532BafNKadPOsGOq1OTwLCS6GNReJ3z7lXvIhDRUOE0s0dsnkjU">

          <div class="form-group">


             <div id="div_id_image" class="form-group">

            <label for="id_image" class="col-form-label ">
                Image
            </label>


          <div class="">
              <input type="file" name="image" accept="image/*" class="clearablefileinput" id="id_image">
           </div>
    </div>
    <div id="instrucciones-adicionales"  style="display:none">

    <p class="bold-font"> Instrucciones adicionales (opcional):</p>

    <div id="div_id_comment" class="form-group">

      <label for="id_comment" class="col-form-label ">
                Comment
      </label>

    <div class="">
       <textarea name="comment" cols="40" rows="10" class="textarea form-control" id="id_comment">
</textarea>

    </div> 
  </div>


  </div>
  </div>


   </br>
   </br>

   <p>O, sáltate este paso y envía tu arte por correo electrónico</p>

   <button type="submit" class="btn btn-naranja text-white btn-block">Continuar
   </button>

</form>

UPDATE 1 : 更新1

The problem was that the call to the JQUery was at the bottom, before body closing tag. 问题在于对JQUery的调用位于主体关闭标签之前的底部。

What should I change in my code, so keeping the call to Jquery at the bottom, it shows the textarea field when user uploads image? 我应该在代码中进行哪些更改,因此将对Jquery的调用保留在底部,当用户上传图像时它会显示textarea字段?

You don't have jQuery enabled on your pen. 您没有在笔上启用jQuery。 You need to add it by hitting the cog icon at the top of the JS pane, and adding it there. 您需要通过单击JS窗格顶部的cog图标并将其添加到其中来添加它。

Even in Codepen, you can use the developer console of your browser to see what errors are being generated by your code. 即使在Codepen中,您也可以使用浏览器的开发人员控制台查看代码正在生成哪些错误。 In this case, you would see: 在这种情况下,您将看到:

Uncaught ReferenceError: $ is not defined

This tells you that jQuery is not being loaded. 这告诉您尚未加载jQuery。

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

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