admin管理员组文章数量:1356734
I am trying to use some JavaScript for a checkbox. When a user makes the checkbox active there will be two fields displayed (This will be hidden soon).
This is the JavaScipt so far for the checkbox:
<script>
function myFunction(cbox) {
if (cbox.checked) {
var input = document.createElement("input");
input.type = "text";
var div = document.createElement("div");
div.id = cbox.name;
div.innerHTML = "Text to display for " + cbox.name;
div.appendChild(input);
document.getElementById("insertinputs").appendChild(div);
} else {
document.getElementById(cbox.name).remove();
}
}
</script>
As you can see when the user clicks the box it will display an input box, however, I cannot seem to get that pre-populated. What I mean in this is, I was using an HTML tag before the javascript witch logged the users Identity.
<input id="CheckedAndVerifiedBy" value="@User.Identity.Name" />
I would like the same thing to happen within the javascript, please help.
I am trying to use some JavaScript for a checkbox. When a user makes the checkbox active there will be two fields displayed (This will be hidden soon).
This is the JavaScipt so far for the checkbox:
<script>
function myFunction(cbox) {
if (cbox.checked) {
var input = document.createElement("input");
input.type = "text";
var div = document.createElement("div");
div.id = cbox.name;
div.innerHTML = "Text to display for " + cbox.name;
div.appendChild(input);
document.getElementById("insertinputs").appendChild(div);
} else {
document.getElementById(cbox.name).remove();
}
}
</script>
As you can see when the user clicks the box it will display an input box, however, I cannot seem to get that pre-populated. What I mean in this is, I was using an HTML tag before the javascript witch logged the users Identity.
<input id="CheckedAndVerifiedBy" value="@User.Identity.Name" />
I would like the same thing to happen within the javascript, please help.
Share Improve this question asked Jun 25, 2018 at 15:04 M.E_M.E_ 871 gold badge1 silver badge13 bronze badges 9-
In your function
myFunction
,input.value = whatever;
should work. Have you tried that? – Guillaume Georges Commented Jun 25, 2018 at 15:07 -
input.value
would be the way to go. But could you clarify what you want? Maybe we can give you a full solution then. – dscham Commented Jun 25, 2018 at 15:12 - @Sens I am trying to create some code so when a user uploads a file some information gets logged such as there User Id and the time it was uploaded. I am also trying to make this hidden. – M.E_ Commented Jun 25, 2018 at 15:25
- @M.E_ I would do that on serverside – dscham Commented Jun 25, 2018 at 15:30
- @Sens Ok, Thanks, How do I go around this? Do you know any good guides? – M.E_ Commented Jun 25, 2018 at 15:32
4 Answers
Reset to default 3input.value = "Prefilled!"
or something like that
If I understand correctly, you are trying to give some value to the input before appending it.
Just do:
...
input.type = "text";
input.value = cbox.name;
var div = document.createElement("div");
...
You're not in any moment assigning a value to input
document.getElementById('your_inpute_fied_id').setAttribute('value', 'the_value_you_wanna-set_to_it');
本文标签: jQueryJavascriptHow to prepopulate a input fieldStack Overflow
版权声明:本文标题:jquery - JavaScript, How to pre-populate a input field? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744064799a2584788.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论