我想添加键盘并单击按钮

时间:2016-09-13 03:01:40

标签: javascript

每当用户按下或单击带有#btnsubmit的按钮时,ajax就会执行。但是当我按下回车键。键盘和点击都会执行 但是当我使用点击时。它只执行点击功能。

<script type="text/javascript">
       function loaddelegates($barcode)
         {
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
              if (this.readyState == 4 && this.status == 200) {
               document.getElementById("getdel").innerHTML = this.responseText;
               $("#btnsubmit").focus();
              }
            };
            xhttp.open("GET", "activityclient/" + $barcode, true);
            xhttp.send();
          }

       function storetime($barcode)
         {
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
              if (this.readyState == 4 && this.status == 200) {
               document.getElementById("getdel").innerHTML = this.responseText;
               $("#barcode").focus().select();
              }
            };
            xhttp.open("GET", "activityclient/" + $barcode + "/store", true);
            xhttp.send();
          }

   $(function() {

         $(document).on('keyup','#barcode',function(e) {
              e.preventDefault();
              var code = e.keyCode || e.which;
              if (code == 13 && $(this).val().length >= 1) {
                  loaddelegates($(this).val());
                  return false;
              }
        });

              $(document).on('keyup','#btnsubmit',function(e) {
              e.preventDefault();
              var code = e.keyCode || e.which;
              if (code == 13) {
                 var barcode = document.getElementById('barcode').value;
                  storetime(barcode);
                  return false;
              }
        });

         $(document).on('click','#btnsubmit',function() {
                 var barcode = document.getElementById('barcode').value;
                  storetime(barcode);
                  return false;
        });
      });

谢谢

1 个答案:

答案 0 :(得分:0)

您不需要为回车键添加事件处理程序。删除它,这应该工作。

button click event is triggered after keypress using jQuery