Javascript jQuery .val 更改不会更改输入值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11873721/
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
jQuery .val change doesn't change input value
提问by Lukas
I have an HTML input with a link in the value.
我有一个 HTML 输入,值中有一个链接。
<input type = 'text' value = 'http://www.link.com' id = 'link' />
I am using jQuery to change the value on a certain event.
我正在使用 jQuery 来更改某个事件的值。
$('#link').val('new value');
The above code changes the value of the text box but doesn't change the value in the code (value = 'http://www.link.com' stays unchanged). I need the value = '' to change as well.
上面的代码改变了文本框的值,但不会改变代码中的值(value = 'http://www.link.com' 保持不变)。我也需要值 = '' 来改变。
回答by Ricardo Alvaro Lohmann
回答by Kevin B
Changing the value property does not change the defaultValue
. In the code (retrieved with .html()
or innerHTML
) the value attribute will contain the defaultValue
, not the value property.
更改 value 属性不会更改defaultValue
. 在代码(用.html()
或检索innerHTML
)中, value 属性将包含defaultValue
,而不是 value 属性。
回答by Landan Hymanson
to expand a bit on Ricardo's answer: https://stackoverflow.com/a/11873775/7672426
扩展一下里卡多的回答:https: //stackoverflow.com/a/11873775/7672426
http://api.jquery.com/val/#val2
http://api.jquery.com/val/#val2
about val()
关于 val()
Setting values using this method (or using the native value property) does not cause the dispatch of the change event. For this reason, the relevant event handlers will not be executed. If you want to execute them, you should call .trigger( "change" ) after setting the value.
使用此方法(或使用本机 value 属性)设置值不会导致更改事件的调度。因此,不会执行相关的事件处理程序。如果你想执行它们,你应该在设置值后调用 .trigger( "change" ) 。
回答by amira
This is just a possible scenario which happened to me. Well if it helps someone then great: I wrote a complicated app which somewhere along the code I used a function to clear all textboxes values before showing them. Sometime later I tried to set a textbox value using jquery val('value') but I did'nt notice that right after that I invoked the ClearAllInputs method.. so, this could also happen.
这只是发生在我身上的一种可能情况。好吧,如果它对某人有帮助,那就太好了:我写了一个复杂的应用程序,在代码的某个地方,我使用了一个函数在显示之前清除所有文本框值。一段时间后,我尝试使用 jquery val('value') 设置文本框值,但在调用 ClearAllInputs 方法之后我没有注意到这一点......所以,这也可能发生。
回答by WiredIn
My similar issue was caused by having special characters (e.g. periods) in the selector.
我的类似问题是由选择器中的特殊字符(例如句点)引起的。
The fix was to escape the special characters:
解决方法是转义特殊字符:
$("#dots\.er\.bad").val("mmmk");
回答by Ivan Jancic
For me the problem was that changing the value for this field didn`t work:
对我来说,问题是更改此字段的值不起作用:
$('#cardNumber').val(maskNumber);
None of the solutions above worked for me so I investigated further and found:
上述解决方案都不适合我,所以我进一步调查并发现:
According to DOM Level 2 Event Specification: The change event occurs when a control loses the input focus and its value has been modified since gaining focus. That means that change event is designed to fire on change by user interaction. Programmatic changes do not cause this event to be fired.
根据 DOM Level 2 事件规范:当控件失去输入焦点并且其值在获得焦点后已被修改时,就会发生更改事件。这意味着更改事件旨在通过用户交互触发更改。编程更改不会导致触发此事件。
The solution was to add the trigger function and cause it to trigger change event like this:
解决方案是添加触发器功能并使其触发更改事件,如下所示:
$('#cardNumber').val(maskNumber).trigger('change');
回答by ikerya
<script src="//code.jquery.com/jquery.min.js"></script>
<script>
function changes() {
$('#link').val('new value');
}
</script>
<button onclick="changes()">a</button>
<input type='text' value='http://www.link.com' id='link'>