问题
I am trying to change the value of the inputfields with JQuery and I have no idea why this doesn't work. It does not throw an error but it just doesn't change. I'm building with appgyver steroids but i don't think that matters.
javascript:
$(document.getElementById("url")).val('test');
html
<input type="text" id="url" class="topcoat-text-input"
style="margin-left:10%; margin-top:10px; width: 80%; text-align: center" autocapitalize="off"
autocorrect="off" required="true"
placeholder="something else"/>
回答1:
Make sure that your code is running after the DOM is loaded.
Also there is no need for jQuery in your example.
You can just do:
window.onload = function(){
document.getElementById("url").value = 'test';
};
Demo: http://jsfiddle.net/qwertynl/7uCfc/
And if you want to do full on jQuery:
$(function(){
$('#url').val('test');
});
Demo: http://jsfiddle.net/qwertynl/m5Ttn/
Or you could not wrap your code in an onload
and just put the whole script at the end of the document after the page has loaded already.
来源:https://stackoverflow.com/questions/20891658/set-value-of-input-element-with-jquery