How do I empty an input value with jQuery?

前端 未结 6 1430
醉话见心
醉话见心 2020-12-23 18:37

I’m trying to make a modal dialog with images where you can select multiple images. I need to get values from an input and then to empty it, but I cannot empty the input. I

相关标签:
6条回答
  • 2020-12-23 19:12

    A better way is:

    $("#element").val(null);
    
    0 讨论(0)
  • 2020-12-23 19:15

    You could try:

    $('input.class').removeAttr('value');
    $('#inputID').removeAttr('value');
    
    0 讨论(0)
  • 2020-12-23 19:17
    $('.reset').on('click',function(){
               $('#upload input, #upload select').each(
                    function(index){  
                        var input = $(this);
                        if(input.attr('type')=='text'){
                            document.getElementById(input.attr('id')).value = null;
                        }else if(input.attr('type')=='checkbox'){
                            document.getElementById(input.attr('id')).checked = false;
                        }else if(input.attr('type')=='radio'){
                            document.getElementById(input.attr('id')).checked = false;
                        }else{
                            document.getElementById(input.attr('id')).value = '';
                            //alert('Type: ' + input.attr('type') + ' -Name: ' + input.attr('name') + ' -Value: ' + input.val());
                        }
                    }
                );
            });
    
    0 讨论(0)
  • 2020-12-23 19:27

    Usual way to empty textbox using jquery is:

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

    If above code is not working than please check that you are able to get the input element.

    console.log($('#txtInput')); // should return element in the console.
    

    If still facing the same problem, please post your code.

    0 讨论(0)
  • 2020-12-23 19:32

    To make values empty you can do the following:

     $("#element").val('');
    

    To get the selected value you can do:

    var value = $("#element").val();
    

    Where #element is the id of the element you wish to select.

    0 讨论(0)
  • 2020-12-23 19:34

    Another way is:

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