Как создать запрос подтверждения ДА / НЕТ перед выполнением сценария копирования? - PullRequest
0 голосов
/ 07 августа 2020

Я плохо разбираюсь в JavaScript, но мой босс запрашивает подтверждение для простого инструмента, который я сделал.

У меня есть скрипты, но я не знаю, как их собрать чтобы он мог работать.

Есть textarea, button для копирования текста из textarea и 3 разных сценария для создания запроса подтверждения перед копированием текста. Если да, скопируйте заметки. Если нет, пусть они завершат sh свои заметки (ничего не делают) ...

Вот демонстрация скрипки

Первый скрипт, который у меня есть, это этот. Это уже работает всякий раз, когда на него нажимают.

function copy1() {
let textarea = document.getElementById("textarea1");
textarea.select();
document.execCommand("copy");
}

Но мне нужно добавить приглашение для создания подтверждения, и вот сценарий для этого:

document.getElementById("promptconfirm").onclick = function() {
confirm("Are you done reviewing your notes?");
};

Теперь мне нужен if else script (который все еще ошибочен)

if (confirm('Are you done reviewing your notes?')) {

console.log('Please try to review your notes before copying.');
} else {

console.log('Please try to review your notes before copying.');
}

Теперь вот HTML-коды

<button class="cbtn" title="Copy to Clipboard" id="promptconfirm" onclick="copy1()"><i class="fa fa-clipboard"></i> Copy</button>

<textarea class="lined" id="textarea1" name="textarea1" spellcheck="true" placeholder="Write something here..." onpaste="console.log('onpastefromhtml')"></textarea>

Я прошу прощения за такую ​​помощь. Я не могу найти другой такой комбинации.

и заранее спасибо ..

document.getElementById("promptconfirm").onclick = function() {
confirm("Are you done reviewing your notes?");
};
        
if (confirm('Are you done reviewing your notes?')) {

console.log('Please try to review your notes before copying.');
} else {

console.log('Please try to review your notes before copying.');
}

function copy1() {
let textarea = document.getElementById("textarea1");
textarea.select();
document.execCommand("copy");
}
<button class="cbtn" title="Copy to Clipboard" id="promptconfirm" onclick="copy1()"><i class="fa fa-clipboard"></i> Copy</button>

<textarea class="lined" id="textarea1" name="textarea1" spellcheck="true" placeholder="Write something here..." onpaste="console.log('onpastefromhtml')"></textarea>

Ответы [ 3 ]

1 голос
/ 07 августа 2020

Просто вызовите copy1, если результатом confirm будет true.

document.getElementById("promptconfirm").onclick = function() {
  if(confirm("Are you done reviewing your notes?")){
    copy1();
  } else {
    console.log('Please try to review your notes before copying.');
  }
};

function copy1() {
  let textarea = document.getElementById("textarea1");
  textarea.select();
  document.execCommand("copy");
}
<button class="cbtn" title="Copy to Clipboard" id="promptconfirm" onclick="copy1()"><i class="fa fa-clipboard"></i> Copy</button>

<textarea class="lined" id="textarea1" name="textarea1" spellcheck="true" placeholder="Write something here..." onpaste="console.log('onpastefromhtml')"></textarea>
1 голос
/ 07 августа 2020

Поскольку execCommand устарело, вот что я бы порекомендовал:

document.getElementById("promptconfirm").addEventListener('click', () => {
  if(confirm("Are you done reviewing your notes?")){
    const textarea = document.getElementById("textarea1");
    const text = textarea.value;
    navigator.clipboard.writeText(text);
  }
})
<button class="cbtn" title="Copy to Clipboard" id="promptconfirm"><i class="fa fa-clipboard"></i> Copy</button>

<textarea class="lined" id="textarea1" name="textarea1" spellcheck="true" placeholder="Write something here..." onpaste="console.log('onpastefromhtml')"></textarea>
0 голосов
/ 07 августа 2020

Может быть вы имеете в виду:

function copy1() {
  let textarea = document.getElementById("textarea1");
  textarea.select();
  document.execCommand("copy");
}

document.getElementById("promptconfirm").onclick = function() {
  if (confirm('Are you done reviewing your notes?')) {
    copy1();
  } else {
    alert('Please try to review your notes before copying.');
  }
};
<button class="cbtn" title="Copy to Clipboard" id="promptconfirm" onclick="copy1()"><i class="fa fa-clipboard"></i> Copy</button>

<textarea class="lined" id="textarea1" name="textarea1" spellcheck="true" placeholder="Write something here..." onpaste="console.log('onpastefromhtml')"></textarea>

Напишите мне в комментариях, вам подходит, если нет, то почему.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...