Compilare un modulo utilizzando javascript

Sep 14 2020

Voglio compilare un modulo quando apro una pagina (è una pagina in cui modifichi le informazioni del tuo profilo).

Ecco il mio codice:

<head>
    <script type="text/javascript">
        function addValues() {
            document.getElementById("datePicker").value = ViewBag.b.DateOfBirth.ToShortDateString();
            document.getElementById("name").value = ViewBag.b.Username;
            document.getElementById("username").value = ViewBag.b.Username;
            document.getElementById("password").value = ViewBag.b.Password;
            document.getElementById("lastname").value = ViewBag.b.Lastname;
        }
    </script>
</head>




<body onload="addValues()">
    <h2>EditProfile</h2>
    <form action="~/Authentication/EditProfile" method="post">
        <table>
            <tr>
                <td>Username:</td>
                <td><input type="text" name="username" id="username" /></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type="text" name="password" /></td>
            </tr>
            <tr>
                <td>Name:</td>
                <td><input type="text" name="name" id="name" /></td>
            </tr>
            <tr>
                <td>Last name:</td>
                <td><input type="text" name="lastname" /></td>
            </tr>
            <tr>
                <td>Date of birth:</td>
                <td><input type="date" name="dateofbirth" id="datePicker" /></td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="Save" />
                </td>
            </tr>
        </table>
    </form>
</body>

Durante il caricamento di una pagina, non compila le informazioni e non riesco a trovare l'errore.

C'è un modo migliore per farlo?

Il JavaScript renderizzato ha il seguente aspetto:

function addValues() {
    document.getElementById("datePicker").value = 11.9.2001. 00:00:00;
    document.getElementById("name").value = Mirko;
    document.getElementById("username").value = micro;
    document.getElementById("password").value = 123456789;
    document.getElementById("lastname").value = Mijanovic;
}

Risposte

1 joncloud Sep 14 2020 at 03:35

Penso che il problema sia una combinazione di cose, come evidenziato in un paio di commenti / risposte.

  • Aggiungi ID a tutti gli input ( passworde lastnamenon hai ID)
  • Assicurati che ViewBagsia accessibile con il @prefisso
  • Assicurati che i valori letterali JavaScript siano visualizzati in modo appropriato

Credo che il codice seguente dovrebbe gestire ViewBage JavaScript letterali:

<script type="text/javascript">
    function addValues() {
        document.getElementById("datePicker").value = '@ViewBag.b.DateOfBirth.ToShortDateString()';
        document.getElementById("name").value = '@ViewBag.b.Username';
        document.getElementById("username").value = '@ViewBag.b.Username';
        document.getElementById("password").value = '@ViewBag.b.Password';
        document.getElementById("lastname").value = '@ViewBag.b.Lastname';
    }
</script>

Notare che tutti i valori letterali sono quotati. Potrebbe essere necessario gestire l'escape aggiuntivo per evitare che una qualsiasi delle ViewBagproprietà causi un errore a causa di un 'valore della proprietà.

3 MiroslavGlamuzina Sep 14 2020 at 03:07

Ti sei perso l'impostazione iddi alcuni <input/>elementi.

<head>
  <script type="text/javascript">
    // Example data
    const ViewBag = {
      b: {
        DateOfBirth: '2020-09-30',
        Username: 'username',
        Password: 'password',
        Lastname: 'lastname'
      }
    };

    function addValues() {
      document.getElementById("datePicker").value = ViewBag.b.DateOfBirth;
      document.getElementById("name").value = ViewBag.b.Username;
      document.getElementById("username").value = ViewBag.b.Username;
      document.getElementById("password").value = ViewBag.b.Password;
      document.getElementById("lastname").value = ViewBag.b.Lastname;
    }
  </script>
</head>


<body onload="addValues()">
  <h2>EditProfile</h2>
  <form action="~/Authentication/EditProfile" method="post">
    <table>
      <tr>
        <td>Username:</td>
        <td><input type="text" name="username" id="username" /></td>
      </tr>
      <tr>
        <td>Password:</td>
        <td><input type="text" name="password" id="password" /></td>
      </tr>
      <tr>
        <td>Name:</td>
        <td><input type="text" name="name" id="name" /></td>
      </tr>
      <tr>
        <td>Last name:</td>
        <td><input type="text" name="lastname" id="lastname" /></td>
      </tr>
      <tr>
        <td>Date of birth:</td>
        <td><input type="date" name="dateofbirth" id="datePicker" /></td>
      </tr>
      <tr>
        <td colspan="2">
          <input type="submit" value="Save" />
        </td>
      </tr>
    </table>
  </form>
</body>

</html>

Spero che sia di aiuto,