2010-02-16 4 views
0

Je suis tryig à lire dans les zones de texte dans un tableau. Comment puis-je le faire? J'ai le code suivant, mais ça ne marche pas.Comment lire les valeurs d'une table dans un texte?

<table id="dataTable"> 
    <thead> 
     <tr> 
      <th>Name</th> 
      <th>Value</th> 
     </tr> 
    </thead> 
    <tr> 
     <td> 
      <input id="Name" type="text" value = "test1" /> 
     </td> 
     <td> 
      <input type="text" value = "test11" /> 
     </td> 
     <td> 
      <input type="button" value = "button11" /> 
     </td> 
    </tr> 
</table> 

Mon script actuel est comme suit:

$(document).ready(function() { 
    $("#Click").click(function() { 
     var i = 0; 
     var inputs = new Array(); 
     $("#dataTable tr").find("input").each(function() { 
      alert($(this.value)); 
      alert($(this.id)); 
      inputs[i] = [this.id, this.value]; 
      i++; 
     }); 

     var columnsCount = $("#dataTable").find("th").length; 
     alert($(columnsCount)); 
    }); 
}); 

<input type="button" id= "Click" value="Click" /> 

Répondre

1

Vous pouvez remplacer

$("#dataTable tr").find("input") 

avec

$("#dataTable tr input:text") 

Il n'y a pas de propriété de valeur et propriété id pour un jQuery objet.

Remplacer

$(this.value) 

avec

$(this).val() 

et

$(this.id) 

avec

$(this).attr ("id") 

Il n'y a pas besoin de $(columnsCount), vous pouvez utiliser columnsCount.

Edité votre code

$(document).ready(function() { 
    $("#Click").click(function() { 
     var inputs = new Array(); 

     $("#dataTable tr input:text").each(function() { 
      inputs.push ([this.id, this.value]); 
     }); 

     var columnsCount = $("#dataTable th").length; 
     alert(columnsCount); 
    }); 
}); 
Questions connexes