-1

嗨,我需要帮助将 textarea 转换为具有三个输入的基本表单,例如...用户可以将他们的名字、姓氏和电子邮件输入到表单中。目前我的代码有一个文本区域,它被存储到 chrome 存储中。请帮我将其转换为一种形式,并且仍然在 chrome 存储中存储和检索它。非常感谢。

html:

    <meta charset="UTF-8">
    <title>Document</title>
    <script src="script.js"></script>
</head>
<body>
    <textarea id="saveLine"></textarea>
    <input type="button" id="save" value="Save Line">

    <input type="button" id="get" value="Get Saved Line">

JS 文件:

window.onload = function() {
    document.getElementById('save').onclick = function() {
    var value = document.getElementById('saveLine').value;

    chrome.storage.sync.set({'myLine': value}, function() {
        alert('success');
    });
};

document.getElementById('get').onclick = function() {
    chrome.storage.sync.get('myLine', function(data) {
        alert(data.myLine);
    });
}
}
4

1 回答 1

0

Simply wrap the HTML inputs/buttons/etc in a form element with an action attribute (though this is optional). You'll also want to change at least one button to be type="submit" in this case.

<form id="some_form" action="/some/endpoint">
  <textarea id="saveLine"></textarea>
  <input type="submit" id="save" value="Save Line">
  <input type="button" id="get" value="Get Saved Line">
</form>

You'll probably also want to handle form submission via your JS because if you want it to also submit to Chrome Storage, then you'll need to hijack the form submit handler.

var form = document.getElementById('some_form')
form.addEventListener('submit', function (event) {
  console.log('Form elements', form.elements)
  event.preventDefault()
  // Do your Chrome Storage stuff here with form elements
})
于 2017-03-01T21:38:34.667 回答