问题
I'm serializing my form using :
formVals = $('#formID').serialize();
But I also have a table that I'd like to serialize and add it to formVals
Can that be done? I've tried :
$('#tableName').find('tr').each(function(){
formVals = $(this).text().serialize();
})
But that results in
Uncaught TypeError: $(...).text(...).serialize is not a function
What I want to do is take the text from each tr
and add it as a new entry to formVals
along with the exiting date. Is there way to do this?
UPDATE
A row looks like this :
<tr id="12354-515-asd">
<td>Today</td>
<td>0000</td>
<td>Monday</td>
<td>2345</td>
</tr>
The expected values to be added to formVals is:
12354-515-asd=Today0000Monday2345
回答1:
You need to concatenate each parameter to formVals
.
formVals = $("#formID").serialize();
$('#tableName').find('tr').each(function(){
formVals += '&' + this.id + '=' + encodeURIComponent($(this).text());
});
回答2:
The reason why you are getting the error "Uncaught TypeError: $(...).text(...).serialize is not a function" is because serialize() is not a function applicable to a "tr" or "td" element (you cannot do this):
http://api.jquery.com/serialize/
The .serialize() method creates a text string in standard URL-encoded notation. It can act on a jQuery object that has selected individual form controls, such as , , and : $( "input, textarea, select" ).serialize();
If you wish to append this data you need to build a string by fetching the ID of the table row, and then iterating through each <td>
element (appending the data within) -- See Barmar's answer for a solid example.
来源:https://stackoverflow.com/questions/40358656/serialize-table-tr-data