Javascript 使用Javascript函数设置输入值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5700471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:31:14  来源:igfitidea点击:

Set Value of Input Using Javascript Function

javascriptinputyuivalidation

提问by Ali Taha Ali Mahboub

I'm currently using a YUI gadget. I also do have a Javascript function to validate the output that comes from the divthat YUI draws for me:

我目前正在使用 YUI 小工具。我也有一个 Javascript 函数来验证来自divYUI 为我绘制的输出:

Event.on("addGadgetUrl", "click", function(){
    var url = Dom.get("gadget_url").value;  /* line x ------>*/
    if (url == "") {
        error.innerHTML = "<p> error" /></p>";
    } else {
        /* line y ---> */ 
        /*  I need to add some code in here to set the value of "gadget_url" by "" */
    }
}, null, true);

Here is my div:

这是我的div

<div>
<p>URL</p>
<input type="text" name="gadget_url" id="gadget_url" style="width: 350px;" class="input"/>
<input type="button" id="addGadgetUrl" value="add gadget"/>
<br>
<span id="error"></span>
</div>

As you can see my question is, how can I set the value of gadget_urlto be ""?

如您所见,我的问题是,如何将 的值设置gadget_url""

回答by Sangeet Menon

Try... for YUI

尝试...为 YUI

Dom.get("gadget_url").set("value","");

with normal Javascript

与正常 Javascript

document.getElementById('gadget_url').value = '';

with JQuery

使用 JQuery

$("#gadget_url").val("");

回答by Calum

document.getElementById('gadget_url').value = '';

回答by Bogdan Mates

The following works in MVC5:

以下在 MVC5 中有效:

document.getElementById('theID').value = 'new value';

回答by thug-gamer

Depending on the usecase it makes a difference whether you use javascript (element.value = x) or jQuery $(element).val(x);

根据用例,使用 javascript ( element.value = x) 还是 jQuery 会有所不同$(element).val(x);

When xis undefinedjQuery results in an empty String whereas javascript results in "undefined"as a String.

xundefinedjQuery的结果在一个空字符串,而在JavaScript的结果"undefined"为一个字符串。

回答by BSounder

I'm not using YUI, but in case it helps anyone else - my issue was that I had duplicate ID's on the page (was working inside a dialog and forgot about the page underneath).

我没有使用 YUI,但如果它可以帮助其他人 - 我的问题是我在页面上有重复的 ID(在对话框中工作并忘记了下面的页面)。

Changing the ID so it was unique allowed me to use the methods listed in Sangeet's answer.

更改 ID 使其唯一允许我使用 Sangeet 的答案中列出的方法。

回答by SMD

document.getElementById('gadget_url').value = 'your value';

document.getElementById('gadget_url').value = '你的价值';

回答by dsky

Solutionprovided by Kamil Kie?czewskiworks perfectly actually. IMHO, much more readable then document.getElementById.

Kamil Kie?czewski提供的解决方案实际上非常有效。恕我直言,比document.getElementById更具可读性。

function setValue(){
gadget_url.value='value to be set';
}
<input id='gadget_url' type='text'/>
<button onclick='setValue()'>Set value</button>

回答by Kamil Kie?czewski

Try

尝试

gadget_url.value=''

addGadgetUrl.addEventListener('click', () => {
   gadget_url.value = '';
});
<div>
  <p>URL</p>
  <input type="text" name="gadget_url" id="gadget_url" style="width: 350px;" class="input" value="some value" />
  <input type="button" id="addGadgetUrl" value="add gadget" />
  <br>
  <span id="error"></span>
</div>

Update

更新

I don't know why so many downovotes (and no comments) - however (for future readers) don't think that this solution not work - It works with html provided in OP question and this is SHORTEST working solution - you can try it by yourself HERE

我不知道为什么有这么多downvotes(并且没有评论) - 但是(对于未来的读者)不要认为这个解决方案不起作用 - 它适用于OP问题中提供的html,这是最短的工作解决方案 - 你可以尝试它自己在这里