簡體   English   中英

如何從此 javascript 代碼中刪除復選框

[英]How to remove checkbox from this javascript code

以下腳本的 function 是從 web 頁面調用“Android Share”選項。 我想刪除復選框,我該怎么做? 我嘗試刪除 html 行,但代碼不起作用。 只有磁貼、文本和 URL 字段是必需的。 沒有復選框和文件上傳選項。

<table>
    <tr><td>Title:</td>
        <td><input type="checkbox" id="title_checkbox" checked/></td>
        <td><input id="title" value="The Title" size="40" /></td>
    </tr>
    <tr><td>Text:</td>
        <td><input type="checkbox" id="text_checkbox" checked/></td>
        <td><input id="text" value="The message" size="40"/></td>
    </tr>
    <tr><td>URL:</td>
        <td><input type="checkbox" id="url_checkbox" checked/></td>
        <td><input id="url" value="https://example.com" size="40"/></td>
    </tr>
    <tr><td>Files:</td>
        <td><!--Without column 2, the whole table is small on mobile.--></td>
        <td><input id="files" type="file" multiple></td>
    </tr>
  </table>
  <p><input id="share" type="button" value="Share" />
     <input id="share-no-gesture" type="button" value="Share without user gesture" /></p>
  <div id="output"></div>
  

這是代碼的rest,javascript

  <script>
    'use strict';

    function sleep(delay) {
      return new Promise(resolve => {
        setTimeout(resolve, delay);
      });
    }

    function logText(message, isError) {
      if (isError)
        console.error(message);
      else
        console.log(message);

      const p = document.createElement('p');
      if (isError)
        p.setAttribute('class', 'error');
      document.querySelector('#output').appendChild(p);
      p.appendChild(document.createTextNode(message));
    }

    function logError(message) {
      logText(message, true);
    }

    function checkboxChanged(e) {
      const checkbox = e.target;
      const textfield = document.querySelector('#' + checkbox.id.split('_')[0]);

      textfield.disabled = !checkbox.checked;
      if (!checkbox.checked)
        textfield.value = '';
    }

    async function testWebShare() {
      if (navigator.share === undefined) {
        logError('Error: Unsupported feature: navigator.share()');
        return;
      }

      const title_input = document.querySelector('#title');
      const text_input = document.querySelector('#text');
      const url_input = document.querySelector('#url');
      const file_input = document.querySelector('#files');

      const title = title_input.disabled ? undefined : title_input.value;
      const text = text_input.disabled ? undefined : text_input.value;
      const url = url_input.disabled ? undefined : url_input.value;
      const files = file_input.disabled ? undefined : file_input.files;

      if (files && files.length > 0) {
        if (!navigator.canShare || !navigator.canShare({files})) {
          logError('Error: Unsupported feature: navigator.canShare()');
          return;
        }
      }

      try {
        await navigator.share({files, title, text, url});
        logText('Successfully sent share');
      } catch (error) {
        logError('Error sharing: ' + error);
      }
    }

    async function testWebShareDelay() {
      await sleep(6000);
      testWebShare();
    }

    function onLoad() {
      // Checkboxes disable and delete textfields.
      document.querySelector('#title_checkbox').addEventListener('click',
          checkboxChanged);
      document.querySelector('#text_checkbox').addEventListener('click',
          checkboxChanged);
      document.querySelector('#url_checkbox').addEventListener('click',
          checkboxChanged);

      document.querySelector('#share').addEventListener('click', testWebShare);
      document.querySelector('#share-no-gesture').addEventListener('click',
          testWebShareDelay);

      if (navigator.share === undefined) {
        if (window.location.protocol === 'http:') {
          // navigator.share() is only available in secure contexts.
          window.location.replace(window.location.href.replace(/^http:/, 'https:'));
        } else {
          logError('Error: You need to use a browser that supports this draft ' +
                   'proposal.');
        }
      }
    }

    window.addEventListener('load', onLoad);
  </script>
</body>
</html>

你可以這樣修復。 您還必須刪除相關的 javascript 代碼。

<table>
    <tr><td>Title:</td>
        <td><input id="title" value="The Title" size="40" /></td>
    </tr>
    <tr><td>Text:</td>
        <td><input id="text" value="The message" size="40"/></td>
    </tr>
    <tr><td>URL:</td>
        <td><input id="url" value="https://example.com" size="40"/></td>
    </tr>
  </table>
  <p><input id="share" type="button" value="Share" />
     <input id="share-no-gesture" type="button" value="Share without user gesture" /></p>
  <div id="output"></div>
    function sleep(delay) {
      return new Promise(resolve => {
        setTimeout(resolve, delay);
      });
    }

    function logText(message, isError) {
      if (isError)
        console.error(message);
      else
        console.log(message);

      const p = document.createElement('p');
      if (isError)
        p.setAttribute('class', 'error');
      document.querySelector('#output').appendChild(p);
      p.appendChild(document.createTextNode(message));
    }

    function logError(message) {
      logText(message, true);
    }

    async function testWebShare() {
      if (navigator.share === undefined) {
        logError('Error: Unsupported feature: navigator.share()');
        return;
      }

      const title_input = document.querySelector('#title');
      const text_input = document.querySelector('#text');
      const url_input = document.querySelector('#url');

      const title = title_input.disabled ? undefined : title_input.value;
      const text = text_input.disabled ? undefined : text_input.value;
      const url = url_input.disabled ? undefined : url_input.value;

      try {
        await navigator.share({title, text, url});
        logText('Successfully sent share');
      } catch (error) {
        logError('Error sharing: ' + error);
      }
    }

    async function testWebShareDelay() {
      await sleep(6000);
      testWebShare();
    }

    function onLoad() {
      document.querySelector('#share').addEventListener('click', testWebShare);
      document.querySelector('#share-no-gesture').addEventListener('click',
          testWebShareDelay);

      if (navigator.share === undefined) {
        if (window.location.protocol === 'http:') {
          // navigator.share() is only available in secure contexts.
          window.location.replace(window.location.href.replace(/^http:/, 'https:'));
        } else {
          logError('Error: You need to use a browser that supports this draft ' +
                   'proposal.');
        }
      }
    }

    window.addEventListener('load', onLoad);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM