Clear text field value in JQuery

前端 未结 15 1406
情书的邮戳
情书的邮戳 2020-12-05 12:26

I understand this is an easy question but for some reason this just isn\'t working for me. I have a function that is triggered everytime a drop down menu is changed. Here

相关标签:
15条回答
  • 2020-12-05 12:59

    In simple way, you can use the following code to clear all the text field, textarea, dropdown, radio button, checkbox in a form.

    function reset(){
        $('input[type=text]').val('');  
        $('#textarea').val(''); 
        $('input[type=select]').val('');
        $('input[type=radio]').val('');
        $('input[type=checkbox]').val('');  
    }
    
    0 讨论(0)
  • 2020-12-05 12:59

        First Name: <input type="text" autocomplete="off" name="input1"/>
        <br/>
        Last Name: <input type="text" autocomplete="off" name="input2"/>
        <br/>
        <input type="submit" value="Submit" />
    </form>
    

    0 讨论(0)
  • 2020-12-05 13:00

    Try using this :

    function checkValue(){
      var value = $('#doc_title').val();
      if (value.length > 0) {
            $('#doc_title').val("");
            $('#doc_title').focus();  // To focus the input 
        }
        else{
        //do something
        }
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <body>
    <input type="text" id="doc_title" value="Sample text"/>
    <button onclick="checkValue()">Check Value</button>
    </body>

    0 讨论(0)
  • 2020-12-05 13:04

    You are comparing doc_val_check with an empty string. You want to assign the empty string to doc_val_check

    so it should be this:

    doc_val_check = "";

    0 讨论(0)
  • 2020-12-05 13:08

    For the most part you just set the .val() method. All of the answers are pretty accurate, just wanted to post my results to anyone who may need to set the value via the client id that gets rendered.

     $('#' + '<%= doc_title.ClientID %>').val(null);
    
    0 讨论(0)
  • 2020-12-05 13:11

    Instead of all those:

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