Clear text field value in JQuery

2019-01-23 10:38发布

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:

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.

14条回答
可以哭但决不认输i
2楼-- · 2019-01-23 10:39

If you want to have element with visible blank text just do this:

$('#doc_title').html(' ');
查看更多
Bombasti
3楼-- · 2019-01-23 10:45

If you want to clear the text field, use:

$('#doc_title').val("");
查看更多
Luminary・发光体
4楼-- · 2019-01-23 10:46

What you need is:

if ($('#doc_title').val()) {
  $('#doc_title').val('');
}
查看更多
淡お忘
5楼-- · 2019-01-23 10:47

We can use this for text clear

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

查看更多
你好瞎i
6楼-- · 2019-01-23 10:50

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);
查看更多
ら.Afraid
7楼-- · 2019-01-23 10:52

Instead of all those:

$('#doc_title').attr("value", "");
查看更多
登录 后发表回答