最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - JavaScript, How to pre-populate a input field? - Stack Overflow

matteradmin8PV0评论

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
 |  Show 4 more ments

4 Answers 4

Reset to default 3
input.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');
Post a comment

comment list (0)

  1. No comments so far