dongtao9095 2013-09-16 12:37
浏览 6
已采纳

关闭时清除表单输入字段

I have a form where I want an optional field to be cleared if the user changes his/her choice. I got the form to add or remove the optional option, but if the user type in text in the optional field, and then changes his/her mind, and hit the cancel button, then I want the value in the optional field to be cleared. I need this because when the form is submitted, it is important that the optional field is empty if no new date from the optional field is ment to be submitted to the database.

    <form>
      <a href="#" class="show_hide">+ Add New</a>
      <div class="slidingDiv">
        Artists name: <input type="text" name="name">
        Artists info: <textarea name="info" cols="65" rows="15"></textarea>  
        <a href="#" class="show_hide">Cancel</a>
      </div>
    </form>

-

$(document).ready(function(){

    $(".slidingDiv").hide();
    $(".show_hide").show();

$('.show_hide').click(function(){
$(".slidingDiv").slideToggle();
});

});

  • 写回答

1条回答 默认 最新

  • dsxsou8465 2013-09-16 12:43
    关注
    $('.show_hide').click(function(){
        $(".slidingDiv").slideToggle();
        $(".slidingDiv input").val("");
        $(".slidingDiv textarea").val("");
        $(".slidingDiv textarea").html("")
    });
    

    This changes the value of the field to "", or blank.

    Note: Textarea's in there twice because I believe some browsers don't record the textarea's content as a value, but as innerHTML.

    Edit: If you want to clear a specific field, assign an id to that field in your HTML, and then just clear its value using jQuery.

    For example:

    <input type="text" name="customerName" id="myInputField" />
    <textarea name="customerResponse" id="myTextarea" /></textarea>
    

    ...and then your jQuery:

    $("#myInputField").val("");
    $("#myTextarea").val("");
    

    So if you gave .slidingDiv input the id "myInputField", and you just wanted to clear the input and NOT the textarea, then your jQuery would look like this:

    $('.show_hide').click(function(){
        $(".slidingDiv").slideToggle();
        $("#myInputField").val("");
    });
    

    This is really, really, really basic jQuery, so make sure you look through some tutorials before coming to StackOverflow for help!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?