简体   繁体   English

jQuery .val 更改不会更改输入值

[英]jQuery .val change doesn't change input value

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).上面的代码改变了文本框的值,但不会改变代码中的值(value = 'http://www.link.com' 保持不变)。 I need the value = '' to change as well.我也需要值 = '' 来改变。

Use attr instead.请改用attr
$('#link').attr('value', 'new value');

demo演示

Changing the value property does not change the defaultValue .更改 value 属性不会更改defaultValue In the code (retrieved with .html() or innerHTML ) the value attribute will contain the defaultValue , not the value property.在代码中(使用.html()innerHTML检索) value 属性将包含defaultValue ,而不是 value 属性。

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.使用此方法(或使用本机 value 属性)设置值不会导致更改事件的调度。 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.如果你想执行它们,你应该在设置值后调用 .trigger( "change" ) 。

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 方法之后我没有注意到这一点......所以,这也可能发生。

$('#link').prop('value', 'new value');

Explanation: Attr will work on jQuery 1.6 but as of jQuery 1.6.1 things have changed.说明:Attr 将在 jQuery 1.6 上工作,但从 jQuery 1.6.1 开始,情况发生了变化。 In the majority of cases, prop() does what attr() used to do.在大多数情况下, prop() 做 attr() 过去做的事情。 Replacing calls to attr() with prop() in your code will generally work.在代码中用 prop() 替换对 attr() 的调用通常会起作用。 Attr will give you the value of element as it was defined in the html on page load and prop gives the updated values of elements which are modified via jQuery. Attr 会给你元素的值,因为它是在页面加载的 html 中定义的,prop 会给出通过 jQuery 修改的元素的更新值。

My similar issue was caused by having special characters (eg periods) in the selector.我的类似问题是由选择器中的特殊字符(例如句点)引起的。

The fix was to escape the special characters:解决方法是转义特殊字符:

$("#dots\\.er\\.bad").val("mmmk");
<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'>

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.根据 DOM Level 2 事件规范:当控件失去输入焦点并且其值在获得焦点后已被修改时,就会发生更改事件。 That means that change event is designed to fire on change by user interaction.这意味着更改事件旨在通过用户交互触发更改。 Programmatic changes do not cause this event to be fired.编程更改不会导致触发此事件。

The solution was to add the trigger function and cause it to trigger change event like this:解决方案是添加触发器功能并使其触发更改事件,如下所示:

$('#cardNumber').val(maskNumber).trigger('change');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM