78

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

这是完整的代码:

$("#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");
                }
            }
        });
    });

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

4

7 回答 7

144

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

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

要获取所选值,您可以执行以下操作:

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

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

于 2012-05-25T13:01:50.003 回答
71

你可以试试:

$('input.class').removeAttr('value');
$('#inputID').removeAttr('value');
于 2012-05-25T13:02:39.187 回答
31

更好的方法是:

$("#element").val(null);
于 2015-07-21T01:24:48.480 回答
16

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

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

如果上面的代码不起作用,请检查您是否能够获取输入元素。

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

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

于 2012-05-25T13:07:07.643 回答
8

另一种方法是:

$('#element').attr('value', '');
于 2015-05-11T13:34:21.370 回答
2
$('.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());
                    }
                }
            );
        });
于 2015-12-19T17:23:47.457 回答
1

对我来说,这是解决这个问题的最好方法:

$('yourElementName').val(null);

于 2021-06-29T12:11:47.833 回答