从特定类中获取输入值并通过jquery发送

问题描述

|
<input id=\"u1\" class=\"username\">
<input id=\"u2\" class=\"username\">
<input id=\"u3\" class=\"username\">
...
如何使用“用户名”类获取输入值,以及如何使用ajax jquery发送到PHP页面。 我想接收简单数组或简单json之类的数据。 (我需要输入值而不是ID)     

解决方法

        
var inputValues = [];
$(\'input.username\').each(function() { inputValues.push($(this).val()); });

// Do whatever you want with the inputValues array
    ,        我发现最好使用jQuery的内置序列化方法。它发送表单数据的方式与常规提交方式相同。您只需给jQuery表单的ID,其余的就交给了jQuery。如果愿意,您甚至可以抓取表单操作。
$.ajax({
  url: \"test.php\",type: \"POST\",data: $(\"#your-form\").serialize(),success: function(data){
    //alert response from server
    alert(data);
  }
});
    ,        
var values = new Array();
$(\'.username\').each(function(){
    values.push( $(this).val());
});