【问题标题】:Javascript function to get input value获取输入值的Javascript函数
【发布时间】:2015-05-04 04:19:43
【问题描述】:

我用多个按钮编写了代码,每个按钮都有不同的值。 我正在尝试在 javascript 函数中获取该值。

  <form action="https://www.bing.com" method="get">
    <input class="input" type="text" name="q" id="field" autofocus>
    <div class="buttons">
      <input type="submit" class="button" id="x1" name="x1" value="Bing"> 
      <input type="submit" class="button" id="x2" name="x2" value="Google">
      <input type="submit" class="button" id="x3" name="x3" value="Yahoo"> 
      <input type="submit" class="button" id="x4" name="x4" value="Duck">
      <input type="submit" class="button" id="x5" name="x5" value="Ask"> 
      <input type="submit" class="button" id="x6" name="x6" value="Aol.">
    </div>
  </form>
</div>

<script>
  var form = document.querySelector('form');
  var input = document.getElementById('field');
    var x = document.getElementById('x1').value;

  form.addEventListener('submit', function(ev) {
    ev.preventDefault();

    if(x=="Bing") {

    var redirect = 'https://google.com/search?q=' + input.value; }

    window.location = redirect;
  });
</script>

这里输入提交只在点击时才设置值对吗?如果我尝试将所有提交按钮 ID 命名为 xx,那么我可以获得在函数中单击的按钮。单击相应的提交按钮后,我想执行不同的重定向。 (多个提交操作: 这个问题不是重复的,是JS和这种情况特有的)

【问题讨论】:

  • 什么是xx?你错过了一些控制……或者……是错字吗?
  • 这个链接javascript-coder.com/html-form/html-form-submit.phtml可以帮助你。简而言之: if($_REQUEST['Operation'] == 'Update') { //Do update here.. } else if($_REQUEST['Operation'] == "Insert") { //Do insert这里}
  • @deostroll 我写这个是为了说 xx 可以是 x1,x2...
  • 我的错,我错过了那一点...... :)

标签: javascript jquery html forms button


【解决方案1】:

试试这样。为我工作

    <input class="input" type="text" name="q" id="field" autofocus>
    <div class="buttons">
      <input type="submit" class="button" id="x1" name="x1" value="Bing"> 
      <input type="submit" class="button" id="x2" name="x2" value="Google">
      <input type="submit" class="button" id="x3" name="x3" value="Yahoo"> 
      <input type="submit" class="button" id="x4" name="x4" value="Duck">
      <input type="submit" class="button" id="x5" name="x5" value="Ask"> 
      <input type="submit" class="button" id="x6" name="x6" value="Aol.">
    </div>

</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>


<script>

$(".button").on("click", function(e){
e.preventDefault();
var x = $(this).attr("value");
var redirect = "" 
if (x=="Google")
{

 window.location = "https://google.com/search?q=" + $(".input").val();

}

});

</script>

扩展 if 条件以获得更多搜索选项。

谢谢

【讨论】:

    【解决方案2】:

    您可以在按钮上使用数据属性并在单击时更新表单操作...

    <form action="https://www.bing.com" method="get">
        <input class="input" type="text" name="q" id="field" autofocus />
        <div class="buttons">
            <input type="submit" class="button" id="x1" name="x1" value="Bing" data-target="https://www.bing.com" /> 
            <input type="submit" class="button" id="x2" name="x2" value="Google" data-target="https://google.com/search?q=" />
        </div>
    </form>
    <script>
    var form = document.querySelector('form');
    var input = document.getElementById('field');
    
    var buttons = document.getElementsByClassName("button");
    for(var i=0; i<buttons.length; i++) {
        buttons[i].addEventListener("click", function(e) {
            form.action = this.dataset.target;
        });
    }
    
    form.addEventListener('submit', function(ev) {
        ev.preventDefault();
        console.log(form.action);
    });
    </script>
    

    jsfiddle - http://jsfiddle.net/1btuez9v/

    【讨论】:

      【解决方案3】:

      脚本将无法按照您编写的方式运行。以下是一些指导方针:

      1. 处理提交按钮的点击事件。将其value 存储在某个地方,比如另一个变量cache
      2. 在表单的提交事件中...检索上述值 (cache.value),然后执行您的重定向逻辑...

      编辑:

      var form = document.querySelector('form');
      var input = document.getElementById('field');
      var cache = {}; //global to store value...
      
      var buttons = document.querySelectorAll('input.button');
      
      for(var x = 0, j = buttons.length; x < j; x++){
          var i = buttons[x];
      
          i.addEventListener('click', function(){        
              cache.value = this.value;        
          }, false);
      }
      
      form.addEventListener('submit', function(ev) {
          ev.preventDefault();
          console.log(cache.value);
      });
      

      这不是最好的方法......但你最终会到达那里;)

      http://jsfiddle.net/1btuez9v/1/(基于Jonathan's小提琴)。

      【讨论】:

      • 我明白了。谢谢。您能否提供一些代码以供开始使用?
      【解决方案4】:

      您可以考虑以下事项:将按钮设为常规按钮&lt;input type="button" ...。然后为每个按钮设置点击事件以执行您想要的任何操作,例如$('#x1').on('click', function(){...});。如果您想在某个时候提交表单,您可以随时使用$('#formid').submit(),它将在文本输入中发送信息。

      也许这可能会有所帮助:http://jsfiddle.net/abalter/boohfpsu/4/

      <form id="myform" action="https://www.bing.com" method="get">
          <input class="input" type="text" name="q" id="field" />
          <div class="buttons">
              <input type="button" class="mybutton" id="bing-button" name="x1" value="Bing"/>
              <input type="button" class="mybutton" id="google-button" name="x2" value="Google"/>
              <input type="button" class="mybutton" id="yahoo-button" name="x3" value="Yahoo"/>
              <input type="button" class="mybutton" id="duck-button" name="x4" value="Duck"/>
              <input type="button" class="mybutton" id="ask-button" name="x5" value="Ask"/>
              <input type="button" class="mybutton" id="Aol-button" name="x6" value="Aol"/>
          </div>
      </form> 
      
      $(document).ready(function(){
          $('#bing-button').on('click', function(){
              alert("bing was clicked");
              alert("the value of the text input is " + $('#field').val());
              alert("I'm now going to submit the form");
              $('#myform').submit();
              alert('submitting the search');
              var search_string = "https://google.com/search?q=" + $('#field').val() + "\"";
              alert("the search string is: " + search_string);
              window.location.href = search_string;
          });
      });
      

      如果您注释掉表单提交,您可以进行谷歌搜索。否则,您将进行 bing 搜索。

      【讨论】:

      • 谢谢。如果我必须为第一个按钮编写代码,你能解释一下吗?另外,是否有其他方法,这种方法可能无法扩展——如果我增加按钮。
      【解决方案5】:

      试试下面的代码应该可以工作

      var input = document.getElementById('field');
      
      
      $('.button').click(function(ev){
         
         if(ev.target.value == "Google"){
            location = "https://google.com/search?q=" + input.value; 
         }//add more cases here with 'else if'
        window.location=location;
      })
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <form action="https://www.bing.com" method="get">
          <input class="input" type="text" name="q" id="field" autofocus>
          <div class="buttons">
            <input type="submit" class="button" id="x1" name="x1" value="Bing"> 
            <input type="submit" class="button" id="x2" name="x2" value="Google">
            <input type="submit" class="button" id="x3" name="x3" value="Yahoo"> 
            <input type="submit" class="button" id="x4" name="x4" value="Duck">
            <input type="submit" class="button" id="x5" name="x5" value="Ask"> 
            <input type="submit" class="button" id="x6" name="x6" value="Aol.">
          </div>
        </form>

      【讨论】:

        猜你喜欢
        • 2018-06-16
        • 1970-01-01
        • 1970-01-01
        • 2019-07-27
        • 1970-01-01
        • 2013-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多