input text tag, after typing value cannot be set

2019-08-01 09:51发布

问题:

I have a text input correctly set in html. When this method is called pressing a button:

this._searchInput.setAttribute("value", "pippo"+Math.random());
console.log(this._searchInput.value);

It works correctly, and input text fields shows the string with random number. Alas, if I type once inside the input text field, all subsequent calls to set value are ignored. Method is called since console outputs the same value visible in text field.

Any suggestion? Is there a reson why after typing in a text field value doesn't get updated by code anymore?

I have already found a similar question but it went unaswered: Cannot set dynamically text value in input after typing

回答1:

You are setting the value attribute of the input, not setting the input's actual value.

This is the line of code you want:

this._searchInput.value= "pippo"+Math.random();