按Enter键时脚本不会结束

时间:2016-01-04 18:06:51

标签: jquery ajax

当我按回车键时,我希望捕获文本区域的值。问题是脚本执行没有敲定,我看不到结果。

<script type="text/javascript">


    $(document).on("ready",function() {
        $("#buscador_texto").keypress(function (event) {


            var keycode = (event.keyCode ? event.keyCode : event.which);

            //alert (keycode);

            if(keycode=="13") {

                //$("#tabla_usuario_individual").show();
                var cod = $('#buscador_texto').val();
                alert (cod);

                $.ajax({
                    type: 'POST',
                    url: '<?php echo site_url("archivo/prueba"); ?>',
                   // data: {'cod':cod}, //enviamos el código por POST

                    success: function(resp) {

                        $("#tabla_usuario_individual").html(resp);


                    }
                });


            };


        });

    });


</script>

2 个答案:

答案 0 :(得分:0)

它是$(document).ready,而不是$(document).on("ready")。 'ready'事件是一个jquery特定事件,只能绑定,而不是委托。

工作示例:https://jsfiddle.net/7a86nfwq/1/

答案 1 :(得分:0)

使用 $(文档).ready(function(){而不是

//outer variable
var answers = {
  name: "hello",
  age: "11"
};

var readline = require('readline');
// var rl = readline.createInterface(process.stdin, process.stdout);
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

function getInput (propertyName, question, cb) {
  var answers = this; //gets the context associated to the current function
  rl.question(question, function(answer) {
    answers[propertyName] = answer;
    rl.close();
    cb();
  });
}

function askForName () {
  console.log(1, answers);
  //sets the execution context of getInput
  getInput.call(answers, "name", "What's your name? ", askForAge);
}

function askForAge () {
  console.log(2, answers);
  //sets the execution context of getInput
  getInput.call(answers, "age", "How old are you? ", printIt);
}

function printIt () {
  console.log("Hello, " + answers.name);
  console.log(".. and " + answers.age + " old.");
}

您可以查看https://jsfiddle.net/xupat5q5/