Как включить / отключить функции JavaScript с помощью флажка? - PullRequest
0 голосов
/ 25 сентября 2019

Hello stackoverflow,

Как бы я мог (временно) отключить / включить функции javascript для a с флажком.

Я хочу иметь возможностьчтобы установить флажок, включающий только одну или обе функции (включая myScrambleFunction и myConvertFunction), myConvertFunction использует заглавные буквы каждой нечетной буквы, а функция myScrambleFunction немного кодирует каждое слово.Иногда я хотел бы использовать только функцию шифрования (без функции заглавных букв) и наоборот.

Любые предложения и примеры будут с благодарностью приняты:)

Это:https://jsfiddle.net/MysteriousDuck/3pndLbgq/3/ - это то, что у меня есть сейчас.

Я пытался найти эту проблему, но не смог найти решение, удовлетворяющее моим потребностям.

function myConvertFunction() {
    var x = document.getElementById("inputText").value;
    var letters = x.split("");
    var string = "";
    for (i = 0; i < letters.length; i++) {
        if (i % 2 == 0) {
            string += letters[i].toUpperCase();
        } else {
            string += letters[i];
        }
    }
    document.getElementById("converted").value = myScrambleFunction(string);
}

function myCopyFunction() {
    var copyText = document.getElementById("converted");
    copyText.select();
    document.execCommand("copy");
    alert("Copied the text: " + copyText.value);
    eraseText();
}

function eraseText() {
    document.getElementById("converted").value = "";
    document.getElementById("inputText").value = "";
    document.getElementById("inputText").focus();
}

function myScrambleFunction(text) {
    let words = text.split(" ");

    words = words.map(word => {
        if (word.length >= 3) {
            return word.split('').sort(() => 0.7 - Math.random()).join('');
        }

        return word;
    });

    return words.join(' ');
}
<body>

    <div class="container">

        <h1> Text Changer </h1>
        <h2> CAPS + randomize letters text changer</h2>

        <div class="checkbox">
            <input type="checkbox" id="checkbox_1">
            <label for="checkbox_1">Caps</label>
        </div>
        <div class="checkbox">
            <input type="checkbox" id="checkbox_2">
            <label for="checkbox_2">Scramble</label>
        </div> 

        <textarea type="text" autofocus="true" placeholder="input text" id="inputText" value="Input Value" spellcheck="false" style="width: 300px;"></textarea>
            <button class="button button1" onclick="myConvertFunction(); myScrambleFunction(text);">Convert</button>
        <textarea type="text" placeholder="CoNvErTeD tExT" id="converted" value="Clear" readonly="true" spellcheck="false" style="width: 300px;"></textarea>
            <button class="button button1" onclick="myCopyFunction(); eraseText();">Copy</button>

    </div>
</body>

1 Ответ

0 голосов
/ 25 сентября 2019

Вам необходимо создать другую функцию для проверки состояния флажка:

var button1Handler = function(e) {
  var checked1 = document.getElementById("checkbox_1").checked,
      checked2 = document.getElementById("checkbox_2").checked;

  if (checked1) myConvertFunction(); 
  if (checked2) myScrambleFunction(text);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...