【问题标题】:Pass the value of a textbox as a parameter to a Javascript function without submit button将文本框的值作为参数传递给没有提交按钮的 Javascript 函数
【发布时间】:2013-01-03 21:29:40
【问题描述】:

我正在写作,因为我在没有提交按钮的情况下通过文本框的值出现问题,但从键盘管理事件。 目标是在我单击 Enter 键时启动该功能。我写了这段代码,但现在它不起作用:

 <form method="GET">
<input type="textfield"  id="testo_libero" onkeydown="javascript: if (event.Keycode==13) desc_ricerca(this.value);">
  </form>

在 Javascript 文件中:

function desc_ricerca(valore){
     console.log("valore"+valore);
 }

谁能帮帮我? 我试过了,但没有...

   function desc_ricerca(valore){    
     var cat = $("#testo_libero").val();
     console.log(cat);
   }

【问题讨论】:

    标签: javascript jtextfield jquery-events keycode


    【解决方案1】:

    看来您正在使用 jQuery。如果是这样,您应该考虑使用unobtrusive JavaScript。 (即使没有 jQuery,你也可以编写不显眼的 JavaScript,但是 jQuery 让这so变得更容易了。

    例如,你的代码可以改写如下:

    HTML:

    <form method="GET">
        <input type="textfield"  id="testo_libero">
    </form>
    

    jQuery:

    $('#testo_libero').on('keydown', function (e) {
        if (e.keyCode != 13) return;
        console.log('valore ' + this.value);
        return false;
    });
    

    这是小提琴:http://jsfiddle.net/wS5Db/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-20
      • 2020-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多