Javascript 清除 JQuery 中的文本字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10951809/
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 03:44:34  来源:igfitidea点击:

Clear text field value in JQuery

javascriptjqueryformsinput

提问by Yuschick

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 is the relevant code that is suppose to grab the current value of the text field, if a value exists, clear it that is contained within the .change function:

我知道这是一个简单的问题,但出于某种原因,这对我不起作用。我有一个每次更改下拉菜单时都会触发的功能。这是假设获取文本字段的当前值的相关代码,如果存在值,则清除它包含在 .change 函数中:

var doc_val_check = $('#doc_title').attr("value");
    if (doc_val_check.length > 0) {
        doc_val_check == "";
    }

I feel like I am missing something very simple.

我觉得我错过了一些非常简单的东西。

回答by thecodeparadox

doc_val_check == "";   // == is equality check operator

should be

应该

doc_val_check = "";    // = is assign operator. you need to set empty value

                       // so you need =

You can write you full code like this:

您可以像这样编写完整的代码:

var doc_val_check = $.trim( $('#doc_title').val() ); // take value of text 
                                                     // field using .val()
    if (doc_val_check.length) {
        doc_val_check = ""; // this will not update your text field
    }

To update you text field with a ""you need to try

要使用""您需要尝试的更新文本字段

$('#doc_title').attr('value', doc_val_check); 
// or 
$('doc_title').val(doc_val_check);

But I think you don't need above process.

但我认为你不需要上述过程。



In short, just one line

简而言之,只有一行

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

Note

笔记

.val()use to set/ get value in text field. With parameter it acts as setter and without parameter acts as getter.

.val()用于在文本字段中设置/获取值。带参数它充当setter,不带参数充当getter。

Read more about .val()

阅读更多关于 .val()

回答by thebiffboff

If you want to clear the text field, use:

如果要清除文本字段,请使用:

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

回答by Tooraj Jam

Instead of all those:

而不是所有这些:

$('#doc_title').attr("value", "");

回答by xdazz

What you need is:

你需要的是:

if ($('#doc_title').val()) {
  $('#doc_title').val('');
}

回答by Sankar

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

回答by Praveen Kumar Purushothaman

If you are using jQuery, then you can use this:

如果你使用 jQuery,那么你可以使用这个:

// var doc_val_check = $('#doc_title').val(); - No need of this!
if ($('#doc_title').val().length > 0) {
    $('#doc_title').val("");
}

回答by Muhammad Kamran

you can clear it by using this line

您可以使用此行清除它

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

回答by Asad Ali

We can use this for text clear

我们可以用它来清除文本

$('input[name="message"]').val("");  

回答by Hrushi

    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>

    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>

回答by SherCoder

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

您正在doc_val_check与空字符串进行比较。您想将空字符串分配给doc_val_check

so it should be this:

所以应该是这样的:

doc_val_check = "";

doc_val_check = "";