Javascript 我想添加keyup并单击一个按钮

Javascript 我想添加keyup并单击一个按钮,javascript,Javascript,每当用户按下或单击带有#btnsupmit的按钮时,ajax就会执行。但当我按下回车键时。keyup和click都会执行。 但是当我使用click时。它只执行单击功能 <script type="text/javascript"> function loaddelegates($barcode) { var xhttp = new XMLHttpRequest(); xhttp.onreadystatec

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

<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;
        });
      });

函数loaddelegates($barcode)
{
var xhttp=newXMLHttpRequest();
xhttp.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
document.getElementById(“getdel”).innerHTML=this.responseText;
$(“#btnsubmit”).focus();
}
};
xhttp.open(“GET”、“activityclient/”+$barcode,true);
xhttp.send();
}
函数存储时间($barcode)
{
var xhttp=newXMLHttpRequest();
xhttp.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
document.getElementById(“getdel”).innerHTML=this.responseText;
$(“#条形码”).focus().select();
}
};
xhttp.open(“GET”、“activityclient/”+$barcode+“/store”,true);
xhttp.send();
}
$(函数(){
$(文档).on('keyup','#barcode',函数(e){
e、 预防默认值();
var代码=e.keyCode | | e.which;
如果(代码==13&&$(this.val().length>=1){
loaddelegates($(this.val());
返回false;
}
});
$(文档).on('keyup','#btnsubmit',函数(e){
e、 预防默认值();
var代码=e.keyCode | | e.which;
如果(代码==13){
var barcode=document.getElementById('barcode')。值;
储存时间(条形码);
返回false;
}
});
$(文档)。在('click','#btnsubmit',函数()上{
var barcode=document.getElementById('barcode')。值;
储存时间(条形码);
返回false;
});
});


谢谢

您不需要为enter键添加事件处理程序。把它取下来,这样就行了


为了澄清,您想按一下enter键触发事件X一次,然后单击相同的按钮触发事件X一次吗?是的。因此,用户只需选择要使用的内容,单击或按enter键即可。