Clear or Reset Button

倖福魔咒の 提交于 2019-12-11 17:55:24

问题


I have a form and a working reset button. When i click the button all inputs and text area boxes are clear. I was wondering if there's a way to create a clear/reset button that will clear only some inputs and not what i have inside my text area box.


回答1:


Here's the jsfiddle solution.

<form>
    <input type="text" class="clearit" /><br />
    <input type="text" class="clearit" /><br />
    <input type="text" class="clearit" /><br />
    <textarea id="t5"></textarea><br />
    <input type="reset" id="reset" />
</form>


$(document).ready(function(){ 
    $('#reset').on('click',function(e){
        e.preventDefault();
        $('.clearit').val("");
    });

});



回答2:


Assign all input elements a class let suppose inputs and different ids let suppose the id of text area is text_area.

<input type  = "textarea " id = "textarea ">

Now with jquery

$(function(){
   $('.inputs').each(function()
   {
        var id = $(this).attr('id');
         if(id == 'textarea '){

         }else{
           $(this).attr('value',"");
         }
   });
})

Done!




回答3:


use jquery it will help u , give the class to fields which u want to clear on click of button and give id to your button

    $('#id_of_button').click(function(){
    $('.input_field_class').val("");
     });



回答4:


See the link

http://www.javascript-coder.com/javascript-form/javascript-reset-form.phtml

Basically rather than calling a form.reset() from the reset button, call an external function which clears out the fields you require and leave the remaining as it is.

Hope it solves your problem!



来源:https://stackoverflow.com/questions/10384050/clear-or-reset-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!