问题描述
我知道这是一个简单的问题,但出于某种原因,这对我不起作用.我有一个每次更改下拉菜单时都会触发的功能.这是假设获取文本字段的当前值的相关代码,如果存在值,则清除它包含在 .change 函数中:
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.
推荐答案
doc_val_check == ""; // == is equality check operator
应该是
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.
$('#doc_title').val("");
注意
.val()
用于设置/获取文本字段中的值.有参数作为setter,没有参数作为getter.
Note
.val()
use to set/ get value in text field. With parameter it acts as setter and without parameter acts as getter.
阅读有关.val()
这篇关于清除 JQuery 中的文本字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!