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 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码