Serialize table tr data?

ぐ巨炮叔叔 提交于 2019-12-25 09:52:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!