为了账号安全,请及时绑定邮箱和手机立即绑定

当我们从文本框中按 Enter 键时,在单个 jQuery 方法中处理多个操作

当我们从文本框中按 Enter 键时,在单个 jQuery 方法中处理多个操作

PHP
PIPIONE 2023-09-15 17:17:29
我想在一个文本框中输入文本后单击回车键时处理两项操作。两个操作是将光标移动到下一个文本框使用php代码建立串口通信$(function() {  $('input:text:first').focus();  var $inp = $('input:text');  $inp.bind('keydown', function(e) {    //var key = (e.keyCode ? e.keyCode : e.charCode);    var key = e.which;    if (key == 13) { // Enter key is pressed      e.preventDefault(); // Prevent the default behaviour of enter key      var nxtIdx = $inp.index(this) + 1;      $(":input:text:eq(" + nxtIdx + ")").focus();      // Ajax query to send request to php      var test = $("#command-text").val();      var inputtext = test;      var command = "1";      // var mode = $(".common-input mb-20").val();      $.ajax({        type: "POST",        url: "controller.php",        data: {          inputtext: inputtext,          command: command        },        cache: false,        success: function(html) {          var feedback = "@" + $("#command-text").val() + "#";          $("#feedback").val(feedback);        }      });    }  });});当我尝试使用以下代码时,仅执行一个操作,光标将移动到下一个文本框或建立通信。请帮我解决这个问题我尝试过的代码如下
查看完整描述

1 回答

?
一只萌萌小番薯

TA贡献1795条经验 获得超7个赞

您使用的语法不正确,括号的位置不正确,请尝试以下操作:


$(function() {

  $('input:text:first').focus();

  var $inp = $('input:text');

  $inp.bind('keydown', function(e) {

    //var key = (e.keyCode ? e.keyCode : e.charCode);

    var key = e.which;

    if (key == 13) { // Enter key is pressed

      e.preventDefault(); // Prevent the default behaviour of enter key

      var nxtIdx = $inp.index(this) + 1;

      $(":input:text:eq(" + nxtIdx + ")").focus();

      // Ajax query to send request to php

      var test = $("#command-text").val();

      var inputtext = test;

      var command = "1";

      // var mode = $(".common-input mb-20").val();

      $.ajax({

        type: "POST",

        url: "controller.php",

        data: {

          inputtext: inputtext,

          command: command

        },

        cache: false,

        success: function(html) {

          var feedback = "@" + $("#command-text").val() + "#";

          $("#feedback").val(feedback);

        }

      });

    }

  });

});


查看完整回答
反对 回复 2023-09-15
  • 1 回答
  • 0 关注
  • 42 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信