Wypełnianie formularza za pomocą JavaScript
Chcę wypełnić formularz podczas otwierania strony (jest to strona, na której edytujesz informacje o swoim profilu).
Oto mój kod:
<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>
Podczas ładowania strony nie wypełnia ona informacji i nie mogę znaleźć błędu.
Czy jest lepszy sposób na zrobienie tego?
Wyrenderowany JavaScript wygląda następująco:
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;
}
Odpowiedzi
Myślę, że problem jest kombinacją rzeczy, co zostało podkreślone w kilku komentarzach / odpowiedziach.
- Dodaj identyfikatory do wszystkich wejść (
passwordilastnamenie mają identyfikatorów) - Upewnij się, że
ViewBagdostęp jest dostępny za pomocą@prefiksu - Upewnij się, że literały JavaScript są prawidłowo renderowane
Uważam, że poniższy kod powinien obsługiwać ViewBagi literały JavaScript:
<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>
Zauważ, że wszystkie wartości literałów są cytowane. Może zajść potrzeba obsłużenia dodatkowych znaków ucieczki, aby zapobiec ViewBagpowodowaniu błędu spowodowanego przez dowolną właściwość 'w wartości właściwości.
Przegapiłeś ustawienie idniektórych <input/>elementów.
<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>
Mam nadzieję że to pomoże,