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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。