.keyup()只工作一次,为什么?

时间:2022-12-06 19:31:11

I am using this pretty simple jquery function, but it seems to work only on the first keyup..

我正在使用这个非常简单的jquery函数,但是它似乎只对第一个键起作用。

$('#cmentuser').keyup(function() {
 var mess = document.getElementById('cmentuser').value;
 var dataString = 'message='+ mess; 

    $.ajax({  
  type: "POST",  
  url: "atuamae.org/comentbyuser.php",  
  data: dataString,  
  success: function() {  
 }  
});
});

any ideas on how to keep it active?

有什么办法让它保持活跃吗?

3 个解决方案

#1


3  

It works, also in the following form (changed mess into jQuery(this).val() and relied on jQuery when encoding the data string):

它的工作方式也如下(将mess改为jQuery(this).val(),在编码数据字符串时依靠jQuery):

$('#cmentuser').keyup(function() {
    $.ajax({  
        type: "POST",  
        url: "atuamae.org/comentbyuser.php",  
        data: {
            'message': jQuery(this).val()
        },  
        success: function() {
            // success callback
        }  
    });
});

Proof that it works: jsfiddle.net/xfxPR/

证明它是有效的:jsfiddle.net/xfxPR/

You may be dynamically changing some elements (eg. changing ID or assuming id does not need to be unique), or maybe unbinding the event. Just make sure the event is being attached and stays attached to the element you need.

您可能正在动态地更改某些元素(如。更改ID或假设ID不需要是唯一的),或者取消绑定事件。只需要确保事件被附加到您需要的元素上,并保持连接。

#2


0  

$(document).on('keyup', '#cmentuser', function(e) {//try to find lower element then doc
   var dataString = 'message='+ $(e.target).val();

   $.ajax({  
      type: "POST",  
      url: "/comentbyuser.php", //no cross domain requests, no need for domain name
      data: dataString,  
      success: function() {}  
   });
});

#3


0  

try this

试试这个

$('#cmentuser').live('keyup',function() {
 var mess = $(this).val();
 var dataString = 'message='+ mess; 
    $.ajax({  
  type: "POST",  
  url: "atuamae.org/comentbyuser.php",  
  data: dataString,  
  success: function() {  
 }  
});
});

#1


3  

It works, also in the following form (changed mess into jQuery(this).val() and relied on jQuery when encoding the data string):

它的工作方式也如下(将mess改为jQuery(this).val(),在编码数据字符串时依靠jQuery):

$('#cmentuser').keyup(function() {
    $.ajax({  
        type: "POST",  
        url: "atuamae.org/comentbyuser.php",  
        data: {
            'message': jQuery(this).val()
        },  
        success: function() {
            // success callback
        }  
    });
});

Proof that it works: jsfiddle.net/xfxPR/

证明它是有效的:jsfiddle.net/xfxPR/

You may be dynamically changing some elements (eg. changing ID or assuming id does not need to be unique), or maybe unbinding the event. Just make sure the event is being attached and stays attached to the element you need.

您可能正在动态地更改某些元素(如。更改ID或假设ID不需要是唯一的),或者取消绑定事件。只需要确保事件被附加到您需要的元素上,并保持连接。

#2


0  

$(document).on('keyup', '#cmentuser', function(e) {//try to find lower element then doc
   var dataString = 'message='+ $(e.target).val();

   $.ajax({  
      type: "POST",  
      url: "/comentbyuser.php", //no cross domain requests, no need for domain name
      data: dataString,  
      success: function() {}  
   });
});

#3


0  

try this

试试这个

$('#cmentuser').live('keyup',function() {
 var mess = $(this).val();
 var dataString = 'message='+ mess; 
    $.ajax({  
  type: "POST",  
  url: "atuamae.org/comentbyuser.php",  
  data: dataString,  
  success: function() {  
 }  
});
});