กรอกแบบฟอร์มโดยใช้จาวาสคริปต์
Sep 14 2020
ฉันต้องการกรอกแบบฟอร์มเมื่อเปิดเพจ (เป็นหน้าที่คุณแก้ไขข้อมูลโปรไฟล์ของคุณ)
นี่คือรหัสของฉัน:
<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>
เมื่อโหลดหน้าเว็บมันไม่ได้กรอกข้อมูลและฉันไม่พบข้อผิดพลาด
มีวิธีที่ดีกว่านี้หรือไม่?
JavaScript ที่แสดงผลมีลักษณะดังนี้:
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;
}
คำตอบ
1 joncloud Sep 14 2020 at 03:35
ฉันคิดว่าปัญหาคือการรวมกันของสิ่งต่างๆดังที่เน้นไว้ในสองความคิดเห็น / คำตอบ
- เพิ่มรหัสให้กับอินพุตทั้งหมด (
passwordและlastnameไม่มีรหัส) - ตรวจสอบให้แน่ใจว่า
ViewBagเข้าถึงได้ด้วย@คำนำหน้า - ตรวจสอบให้แน่ใจว่าตัวอักษร JavaScript แสดงผลอย่างเหมาะสม
ฉันเชื่อว่ารหัสต่อไปนี้ควรจัดการViewBagและตัวอักษร 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>
โปรดทราบว่าค่าตัวอักษรทั้งหมดจะถูกยกมา คุณอาจต้องจัดการกับค่า Escape เพิ่มเติมเพื่อป้องกันไม่ให้ViewBagคุณสมบัติใด ๆทำให้เกิดข้อผิดพลาดเนื่องจาก'ค่าคุณสมบัติ
3 MiroslavGlamuzina Sep 14 2020 at 03:07
คุณพลาดการตั้งค่าidสำหรับ<input/>องค์ประกอบบางอย่าง
<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>
หวังว่านี่จะช่วยได้