在JS中连接点击的按钮值和功能

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在JS中连接点击的按钮值和功能相关的知识,希望对你有一定的参考价值。

我是JS的初学者。

我有2个功能。怎么做 :

  1. 如果我点击第一个按钮,使第一个功能工作,如果我点击第二个按钮,使第二个功能工作?
  2. 将按下的按钮值应用于功能,然后将其应用于输入字段。

示例:当我使用Caesar Cipher键入'ABC'时,它将返回'NOP',当我使用我的密码(或任何其他)键入'ABC'时,它将返回'BCD'(或任何其他值,它取决于哪个密码被选中)。提前谢谢大家

我的Js和html代码如下:

<div class="container">
  <div class="row">
    <form  class="col s12 m12 l12">
    <h2>JS Encription</h2>
      <div class="row">
        <div class="input-field col s5 m5 l5">
          <input id="field1" placeholder="Type you text here" id="first_name" type="text" class="validate">
          <label for="first_name">Input</label>
        </div>
        <div class="input-field col s5 m5 l5">
          <input id="field2" disabled placeholder="Result is shown here" id="first_name" type="text" class="validate">
          <label for="first_name">Output</label>
        </div>
      </div>
    </form>

  </div>
      <div class="row switchBtns">
        <div class="col s12 m12 l12">
            <div id="caesarButton" class="col s3 m3 l3 ">
                <a class="waves-effect waves-light btn-small">Caesar Cipher</a>
            </div>
            <div id="mineButton" class="col s3 m3 l3 ">
                <a class="waves-effect waves-light btn-small">My Cipher</a>
            </div>
            <div class="col s3 m3 l3 ">
                <a class="waves-effect waves-light btn-small">3rd Variant</a>
            </div>
            <div class="col s3 m3 l3 ">
                <a class="waves-effect waves-light btn-small">4th Variant</a>
            </div>
        </div>
    </div>
</div>

和JS代码

 // CAESAR
        $("#caesarButton").click(function() {
            var clicked = $(this).val();
            $('#field1').val(encryp(clicked)).val();
        });

        $('#field1').on('keyup keypress blur', function () {  
            var textvalue = $(this).val();
            $('#field2').val(encryp(textvalue)).val();  
        });


        function encryp(tekst) {
            var result = "";
            var str = tekst.toUpperCase();


            for (var i=0; i<str.length ; i++) {
                var ascii = str[i].charCodeAt();

                if(ascii>=65 && ascii<=77) {
                    result+=String.fromCharCode(ascii+13);
                }
                else if(ascii>=78 && ascii<=90) {
                    result+=String.fromCharCode(ascii-13);
                }
                else {
                    result+=" ";
                }
            }
            return result ;
        }

        //MINE
        $("#mineButton").click(function() {
            var clicked = $(this).val();
            $('#field1').val(encryp(clicked)).val();
        });

        $('#field1').on('keyup keypress blur', function () {  
            var textvalue = $(this).val();
            $('#field2').val(encryp(textvalue)).val();  
        });


        function encryp(tekst) {
            var result = "";
            var str = tekst.toUpperCase();


            for (var i=0; i<str.length ; i++) {
                var ascii = str[i].charCodeAt();

                if(ascii>=65 && ascii<=77) {
                    result+=String.fromCharCode(ascii+3);
                }
                else if(ascii>=78 && ascii<=90) {
                    result+=String.fromCharCode(ascii-3);
                }
                else {
                    result+=" ";
                }
            }
            return result ;
        }
答案

我为你做了一个简单的上下文切换输入处理函数的例子,在这里你去:

// use an object as a key-value store for your functions
var funcs = {
  caesar: encryp1, // i dont know if i got these the right way around :D
  mine: encryp2
}
// store which funciton is currently selected in a variable
var selected = "caesar"

// call this function just to show the default selected function
determineOutput()

// CAESAR
$("#caesarButton").click(function() {
  console.log("caesar button clicked!")
  // here assign which function to use
  selected = "caesar"
  determineOutput()
});

//MINE
$("#mineButton").click(function() {
  console.log("mine button clicked!")
  // here assign which function to use
  selected = "mine"
  determineOutput()
});

$('#field1').on('keyup keypress blur', function () {  
  // this function is alled every time one of the events 
  // listed happens on the #field1 element
  determineOutput()
});

function determineOutput(){
  var textvalue = $('#field1').val();
  //use the function currently selected by addressing it with 
  // [] on the funcs object
  var correctFunction = funcs[selected]
  // then call the selected function with the input text
  $('#field2').val(correctFunction(textvalue));  
  // show the user which cipher we're using
  $("#currentCipher").html("current cipher:" + selected)
}


function encryp1(tekst) {
  var result = "";
  var str = tekst.toUpperCase();


  for (var i=0; i<str.length ; i++) {
    var ascii = str[i].charCodeAt();

    if(ascii>=65 && ascii<=77) {
      result+=String.fromCharCode(ascii+13);
    }
    else if(ascii>=78 && ascii<=90) {
      result+=String.fromCharCode(ascii-13);
    }
    else {
      result+=" ";
    }
  }
  return result ;
}

function encryp2(tekst) {
  var result = "";
  var str = tekst.toUpperCase();


  for (var i=0; i<str.length ; i++) {
    var ascii = str[i].charCodeAt();

    if(ascii>=65 && ascii<=77) {
      result+=String.fromCharCode(ascii+3);
    }
    else if(ascii>=78 && ascii<=90) {
      result+=String.fromCharCode(ascii-3);
    }
    else {
      result+=" ";
    }
  }
  return result ;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <input id="field1" placeholder="Type you text here" type="text">
  <label for="field1">Input</label>
</div>

<div>
  <input id="field2" disabled placeholder="Result is shown here" type="text" class="validate">
  <label for="field2">Output</label>
</div>

<p id="currentCipher"></p>

<button id="caesarButton">
  Caesar Cipher
</button>

<button id="mineButton">
  My Cipher
</button>

以上是关于在JS中连接点击的按钮值和功能的主要内容,如果未能解决你的问题,请参考以下文章

如何将按钮功能添加到片段中

可点击的 div 充当按钮功能 react js

如何使用onclick一个带有多个按钮php的插入功能的按钮

添加到片段时按钮不起作用

在 android Studio 中,我希望当我点击按钮时,下一个活动/片段应该来自右侧

js网页点击“按钮”实现“键盘按下Tab键”功能函数?