Javascript 如何使用 jQuery 清空输入值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10754874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:47:57  来源:igfitidea点击:

How do I empty an input value with jQuery?

javascriptjqueryhtmljquery-ui

提问by Vit Kos

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 tried .val('')and .val(null), but neither worked for me.

我正在尝试使用图像制作一个模态对话框,您可以在其中选择多个图像。我需要从输入中获取值然后清空它,但我无法清空输入。我试过.val('').val(null),但都不适合我。

Here is the full code:

这是完整的代码:

$("#hdselect").click(function(){
        $(".modal").html("");

        $.post('mediaservice.php',{hd:'ok',images:$("#hdimages").val()},function(data){
            $(".modal").append(data);
        });
        $(".modal").dialog({
            'modal':true,
            'title':"Click the image to select",
            'width':960,
            'height':600,
            'resizable':false,
            'show': {effect: 'drop', direction: "up"},
            'buttons': {"Ok": function() {  
                    var hd=Array();
                    var hdval=$("#hdimages").val();
                    $("#hdimages").attr('value',' ');
                    $("input[name='hd[]']:checked").each(function(){
                        hd.push($(this).val());
                    });
                    if(hdval!=''){
                        hdval=hdval+","+hd;
                    }else{
                        hdval=hd;
                    }                        
                    $("#hdimages").val(hdval);
                    var images=$("#hdimages").val();
                    $.post('mediaservice.php',{getHd:images},function(data){
                        $("#imgthumbBase").append(data);
                    });
                    $(this).dialog("close");
                }
            }
        });
    });

The idea is that the user clicks a button and a modal dialog opens with multiple images and checkboxes. At this point I need to get the values from an input, and then clear it.

这个想法是用户点击一个按钮,一个带有多个图像和复选框的模式对话框打开。此时我需要从输入中获取值,然后将其清除。

采纳答案by Brandon Ferrara

You could try:

你可以试试:

$('input.class').removeAttr('value');
$('#inputID').removeAttr('value');

回答by Darren

To make values empty you can do the following:

要使值为空,您可以执行以下操作:

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

To get the selected value you can do:

要获得选定的值,您可以执行以下操作:

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

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

#element您要选择的元素的 id在哪里。

回答by Arjun Tuli

A better way is:

更好的方法是:

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

回答by shanky

Usual way to empty textbox using jquery is:

使用 jquery 清空文本框的通常方法是:

$('#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.

如果仍然面临同样的问题,请发布您的代码。

回答by CIRCLE

Another way is:

另一种方式是:

$('#element').attr('value', '');

回答by raji

$('.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());
                    }
                }
            );
        });