简体   繁体   English

用jQuery设置输入元素的值

[英]set value of input element with jquery

I am trying to change the value of the inputfields with JQuery and I have no idea why this doesn't work. 我试图用JQuery更改输入字段的值,但我不知道为什么这不起作用。 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. 我正在使用appgyver类固醇进行制造,但我认为这并不重要。

javascript: JavaScript的:

$(document.getElementById("url")).val('test');

html 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"/>

Make sure that your code is running after the DOM is loaded. 确保在加载DOM后您的代码正在运行。

Also there is no need for jQuery in your example. 同样,在您的示例中也不需要jQuery。

You can just do: 您可以这样做:

window.onload = function(){
    document.getElementById("url").value = 'test';
};

Demo: http://jsfiddle.net/qwertynl/7uCfc/ 演示: http : //jsfiddle.net/qwertynl/7uCfc/


And if you want to do full on jQuery: 而且,如果您想在jQuery上做完整的事情:

$(function(){
    $('#url').val('test');
});

Demo: http://jsfiddle.net/qwertynl/m5Ttn/ 演示: 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. 或者你可以 换你的代码中的onload ,只是把整个脚本在文件末尾的页面已经加载之后。

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

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