Reset textbox value in javascript

后端 未结 9 1005
执笔经年
执笔经年 2020-12-13 17:16

If I have a input textbox like this:


How can I set the value of the t

相关标签:
9条回答
  • 2020-12-13 17:31

    To set value

     $('#searchField').val('your_value');
    

    to retrieve value

    $('#searchField').val();
    
    0 讨论(0)
  • 2020-12-13 17:34

    Use it like this:

    $("#searchField").focus(function() {
        $(this).val("");
    });
    

    It has to work. Otherwise it probably never gets focused.

    0 讨论(0)
  • 2020-12-13 17:34

    This worked for me:

    $("#searchField").focus(function()
    { 
        this.value = ''; 
    });
    
    0 讨论(0)
  • 2020-12-13 17:35

    In Javascript :

    document.getElementById('searchField').value = '';
    

    In jQuery :

    $('#searchField').val('');
    

    That should do it

    0 讨论(0)
  • 2020-12-13 17:41

    First, select the element. You can usually use the ID like this:

    $("#searchField"); // select element by using "#someid"
    

    Then, to set the value, use .val("something") as in:

    $("#searchField").val("something"); // set the value
    

    Note that you should only run this code when the element is available. The usual way to do this is:

    $(document).ready(function() { // execute when everything is loaded
        $("#searchField").val("something"); // set the value
    });
    
    0 讨论(0)
  • 2020-12-13 17:45

    With jQuery, I've found that sometimes using val to clear the value of a textbox has no effect, in those situations I've found that using attr does the job

    $('#searchField').attr("value", "");
    
    0 讨论(0)
提交回复
热议问题