How to cancel or reset specific form values?

后端 未结 5 1986
感情败类
感情败类 2021-01-23 00:29

I have this code


  
    

<
5条回答
  •  情书的邮戳
    2021-01-23 00:59

    This can be done by using this code:

    document.getElementById('button').onclick = function() { 
      form.elements['c'].value = form.elements['c'].defaultValue;
    }
    

    But first make sure you give your element an ID of button, such as:

    
    

    By using this code, you're basically saying any element with an attribute of c, within this form, to reset to the default value which it had.

    See this jsFiddle.

    This will work with your example code. However, it probably wont if you have multiple items that need resetting; this is where Javascript gets messy.

    jQuery will be much simpler to use for multiple items which need resetting to default value.

    Here's a simple way to set the value in jQuery:

    $('input.reset').live('click', function() {
    
       $('input.3').val('3');
    
    });
    

    Where as you need to define the value you want to have in this jQuery code, jQuery is very simple to understand. A

    See this jsFiddle.

提交回复
热议问题