亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 javascript 填寫表單

使用 javascript 填寫表單

qq_笑_17 2023-03-03 16:00:24
我想在打開頁面時填寫表格(這是您編輯個人資料信息的頁面)。這是我的代碼:<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;}
查看完整描述

2 回答

?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

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>


查看完整回答
反對 回復 2023-03-03
?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

我認為問題是多種因素的結合,正如一些評論/答案中所強調的那樣。

  • 將 ID 添加到所有輸入(password并且lastname沒有 ID)

  • 確保ViewBag使用@前綴訪問

  • 確保正確呈現 JavaScript 文字

我相信以下代碼應該處理ViewBagJavaScript 文字:

<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>

請注意,所有文字值都被引用了。您可能需要處理額外的轉義,以防止任何屬性ViewBag由于'屬性值中的 a 而導致錯誤。


查看完整回答
反對 回復 2023-03-03
  • 2 回答
  • 0 關注
  • 230 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號