set value of input element with jquery

旧城冷巷雨未停 提交于 2020-01-17 01:06:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!