问题
So, as some browsers auto-fill sign in forms, it causes an issue with my interactive placeholders. The placeholder is a <span>
element with some text in, which moves above the input itself when focussed. Upon the browser inserting the data it has 'remembered', how would I be able to detect its presence, and it's value?
Please note, that it is the auto-completed value of the input which I wish to grab.
回答1:
Using the following code, I have managed to achieve the result I wished for:
setTimeout(function() {
console.log($('#input').val());
});
Using a timeout allowed the browser to load itself prior to the code requesting data from it.
回答2:
Give the span an ID and run the text() method on it. Without any parameters it works as a getter.
var yourtext = $('#somespan').text();
If it was an actual textfield instead of a span, you would get the contents with the .val() method.
var yourtext = $( "#foo" ).val();
来源:https://stackoverflow.com/questions/34098975/getting-the-value-of-autofilled-inputs-jquery