Text box not getting reset

后端 未结 4 1890
没有蜡笔的小新
没有蜡笔的小新 2021-01-21 23:47

I have a text box and I tried to reset it after a button click. But it is not getting reset. HTML Script:-

    

        
相关标签:
4条回答
  • 2021-01-22 00:09

    To clear the value of input box use :

      document.getElementById('pre-selected-options').value = "";
    

    https://www.w3schools.com/howto/howto_html_clear_input.asp

    0 讨论(0)
  • 2021-01-22 00:11

    The trouble you are facing here is that you are trying to clear the input box with id inp while calling pre-selected-options, which isn't connected to your text box.

    Using document.getElementById('inp').value = "" should clear the value of your textbox.

    0 讨论(0)
  • 2021-01-22 00:16

    To reset form by using jQuery.

    $("form")[0].reset();
    
    0 讨论(0)
  • 2021-01-22 00:23

    To reset a form, use

    form.reset();
    

    var form = document.querySelector('form');
    var button = document.querySelector('button');
    
    button.addEventListener('click', function() {
      form.reset();
    })
    <form>
      <input value="volvo">
    
      <select>
        <option value="volvo">Volvo</option>
        <option value="saab">Saab</option>
        <option value="mercedes">Mercedes</option>
        <option value="audi">Audi</option>
      </select>
    
      <button type="button">Reset form</button>
    </form>


    To reset a value on e.g. input, use

    input.value = '';
    

    var input = document.querySelector('input');
    var button = document.querySelector('button');
    
    button.addEventListener('click', function() {
      input.value = '';
    })
    <input value="volvo">
    
    
    <button>Reset to none</button>


    To reset a value on e.g. select, use

    select.selectedIndex = 0;    // select first option
    select.selectedIndex = -1;   // select none
    

    var select = document.querySelector('select');
    var button = document.querySelector('button');
    
    button.addEventListener('click', function() {
      select.selectedIndex = -1;   // select none
    })
    <select>
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="mercedes">Mercedes</option>
      <option value="audi">Audi</option>
    </select>
    
    
    <button>Reset Select to none</button>

    0 讨论(0)
提交回复
热议问题