用户输入变量到文本的值

时间:2017-04-21 04:08:31

标签: javascript jquery

我尝试做的是,当用户输入的值与我想要输入的值的两个变量中的一个匹配时,然后触发'变量。所以基本上,你在提示中输入变量的名称,然后我想要一个段落标记来获取变量所持有的文本。

然而,由于输入的值成为文本而根本不触发变量,因此我无法使其工作。它只是转换成纯文本。

我希望我提供的片段能够清除我想要实现的其余部分!



button {
  transition:color .3s;
  border:none;
  font-size:18px;
  color:white;
  padding:1%;
  width:100px;
  cursor:pointer;
}

button:hover {
  color:rgba(0,0,0,.7)
}

.go {
  background-color:#4CAF50;
}

.reset {
  background-color:#f44336;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="reset">Reset</button>

<button class="go">Go</button>

<p>Replace me</p>
&#13;
typedef struct {int a; int b;} A_t;
A_t AA;
AA.a = 3; AA.b = 4;
// compilation fails here
A_t& BB = AA;
&#13;
&#13;
&#13;

5 个答案:

答案 0 :(得分:2)

  var decision={
    help : 'Put me in the paragraph tag please!',
    no : 'Or me, either way it\'s fine!'
  }


  $('.reset').on('click', function() {
    $('p').text('');
  });

  $('.go').on('click', function() {
	var key = prompt('Enter something bro').toLowerCase();
    
    $('p').text(decision[key]);


  });
button {
  transition:color .3s;
  border:none;
  font-size:18px;
  color:white;
  padding:1%;
  width:100px;
  cursor:pointer;
}

button:hover {
  color:rgba(0,0,0,.7)
}

.go {
  background-color:#4CAF50;
}

.reset {
  background-color:#f44336;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="reset">Reset</button>

<button class="go">Go</button>

<p>Replace me</p>

这是针对您的问题的简单解决方案。存储您的选项不是作为变量而是作为对象存储。您可以使用对象键获取选项的值。或者直接获取对象的属性。有关详细信息,请参阅Objects and Properties

答案 1 :(得分:0)

使用全局窗口输出值

&#13;
&#13;
var help = 'Put me in the paragraph tag please!'
  var no = 'Or me, either way it\'s fine!'


  $('.reset').on('click', function() {
    $('p').text('');
  });

  $('.go').on('click', function() {
	var key = prompt('Enter something bro').toLowerCase();
    
    $('p').text(window[key]);


  });
&#13;
button {
  transition:color .3s;
  border:none;
  font-size:18px;
  color:white;
  padding:1%;
  width:100px;
  cursor:pointer;
}

button:hover {
  color:rgba(0,0,0,.7)
}

.go {
  background-color:#4CAF50;
}

.reset {
  background-color:#f44336;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="reset">Reset</button>

<button class="go">Go</button>

<p>Replace me</p>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

使用window[key]

    var help = 'Put me in the paragraph tag please!'
      var no = 'Or me, either way it\'s fine!'


      $('.reset').on('click', function() {
        $('p').text('');
      });

      $('.go').on('click', function() {
    	var key = prompt('Enter something bro').toLowerCase();
    if(window[key] != undefined )
                alert(window[key]);
            $('p').text(window[key]);
      });
    button {
      transition:color .3s;
      border:none;
      font-size:18px;
      color:white;
      padding:1%;
      width:100px;
      cursor:pointer;
    }

    button:hover {
      color:rgba(0,0,0,.7)
    }

    .go {
      background-color:#4CAF50;
    }

    .reset {
      background-color:#f44336;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button class="reset">Reset</button>

    <button class="go">Go</button>

    <p>Replace me</p>

答案 3 :(得分:0)

<script>
$(document).ready(function(){
var help = 'Put me in the paragraph tag please!'
  var no = 'Or me, either way it\'s fine!'

  $('.reset').on('click', function() {
  
    $('p').text('');
  });

  $('.go').on('click', function() {
	var key = prompt('Enter something bro').toLowerCase();
   
    $('p').text(key);

  });
});
  </script>

你必须使用document.ready函数来执行jquery代码..

答案 4 :(得分:0)

使用window[key]使用输入文本与变量

进行比较

&#13;
&#13;
var help = 'Put me in the paragraph tag please!'
  var no = 'Or me, either way it\'s fine!'


  $('.reset').on('click', function() {
    $('p').text('');
  });

  $('.go').on('click', function() {
	var key = prompt('Enter something bro').toLowerCase();
    var name = window[key];
    $('p').text(name);


  });
&#13;
button {
  transition:color .3s;
  border:none;
  font-size:18px;
  color:white;
  padding:1%;
  width:100px;
  cursor:pointer;
}

button:hover {
  color:rgba(0,0,0,.7)
}

.go {
  background-color:#4CAF50;
}

.reset {
  background-color:#f44336;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="reset">Reset</button>

<button class="go">Go</button>

<p>Replace me</p>
&#13;
&#13;
&#13;