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

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

將 html 表單的響應下載到文本文件

將 html 表單的響應下載到文本文件

白衣非少年 2024-01-11 17:36:07
我有一個 HTML 表單,其中有幾個問題。當用戶單擊“提交”時回答每個問題后,我希望將響應下載為文本文件或 pdf 文件。我也沒有在該文件中寫入“null”。<form class="contact100-form validate-form">  <div id="source">    <div class="wrap-input100 validate-input" required="required">      <span class="label-input100"><h4>Name</h4></span>      <input        id="source"        class="input100"        type="text"        name="name"        placeholder="enter your full name..."      />    </div>    <div class="wrap-input100 validate-input" required="required">      <span class="label-input100"><h4>Your Birthday</h4></span>      <input        id="source"        class="input100"        type="text"        name="name"        placeholder="write your complete DOB dd-mm-yyyy..."      />    </div>    <div class="wrap-input100 validate-input" required="required">      <span class="label-input100"><h4>Contact Number (Primary)</h4></span>      <input        id="source"        class="input100"        type="text"        name="name"        placeholder="this is most important..."      />    </div>    <div class="container-contact100-form-btn">      <div class="wrap-contact100-form-btn">        <div class="contact100-form-bgbtn"></div>        <button type="button" id="save" title="Save as text file">          Send        </button>      </div>    </div>  </div></form><script type="text/javascript">      // when document is ready      document.getElementById("save").onclick = function () {        // when clicked the button        var content = document.getElementById("source").getAttribute("value");        // a [save as] dialog will be shown        window.open(          "data:application/txt," + encodeURIComponent(content),          "_self"        );      };    </script>
查看完整描述

2 回答

?
慕姐4208626

TA貢獻1852條經驗 獲得超7個贊

.getAttribute("value")

value屬性保存元素的默認值。

當前值(即用戶輸入的值)保存在 value屬性中。

使用.value。


查看完整回答
反對 回復 2024-01-11
?
慕斯709654

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

每個輸入都需要不同的 ID(和名稱)。


您還需要將 .getAttribute("value") 替換為 .value


以下代碼有效:


<form class="contact100-form validate-form">

? ? <div id="source">

? ? ? ? <div class="wrap-input100 validate-input" required="required">

? ? ? ? ? ? <span class="label-input100"><h4>Name</h4></span>

? ? ? ? ? ? <input id="name" class="input100" type="text" name="name" placeholder="enter your full name..." />

? ? ? ? ?</div>


? ? ? ? <div class="wrap-input100 validate-input" required="required">

? ? ? ? ? ? <span class="label-input100"><h4>Your Birthday</h4></span>

? ? ? ? ? ? <input id="birthday" class="input100" type="text" name="birthday" placeholder="write your complete DOB dd-mm-yyyy..." />

? ? ? ? </div>


? ? ? ? <div class="wrap-input100 validate-input" required="required">

? ? ? ? ? ? <span class="label-input100"><h4>Contact Number (Primary)</h4></span>

? ? ? ? ? ? <input id="contactNb" class="input100" type="text" name="contactNb" placeholder="this is most important..." />

? ? ? ? </div>


? ? <div class="container-contact100-form-btn">

? ? ? ? <div class="wrap-contact100-form-btn">

? ? ? ? ? ? ?<div class="contact100-form-bgbtn"></div>

? ? ? ? ? ? ? ? <button type="button" id="save" title="Save as text file">Send</button>

? ? ? ? ? ? </div>

? ? ? ? </div>

? ? </div>

</form>


<script type="text/javascript">

? ? document.getElementById("save").onclick = function () {

? ? ? ? var name = document.getElementById('name').value;

? ? ? ? var birthday = document.getElementById('birthday').value;

? ? ? ? var contactNb = document.getElementById('contactNb').value;


? ? ? ? var content = " name: " + name + "\n birthday: " + birthday + "\n contact number: " + contactNb?


? ? ? ? window.open( "data:application/txt," + encodeURIComponent(content), "f.txt" );

? ? };

</script>

查看完整回答
反對 回復 2024-01-11
  • 2 回答
  • 0 關注
  • 186 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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