欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

HTML: Is it possible to have a FORM tag in each TABLE ROW in a XHTML valid way? form 不能作为 table 的子元素 有大用

I can best describe this as follows:

I want this (entire table in editmode and save button in every row).

<table>
    <tr>
        <td>Id</td>
        <td>Name</td>
        <td>Description</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td><input type="hidden" name="id" value="1" /></td>
        <td><input type="text" name="name" value="Name" /></td>
        <td><input type="text" name="description" value="Description" /></td>
        <td><input type="submit" value="Save" /></td>
    </tr>
    <tr>
        <td><input type="hidden" name="id" value="2" /></td>
        <td><input type="text" name="name" value="Name2" /></td>
        <td><input type="text" name="description" value="Description2" /></td>
        <td><input type="submit" value="Save" /></td>
    </tr>
    <!-- and more rows here ... -->
</table>

Where should I put the <form> tags?

 

正确答案

将表格包裹在表单元素中:

<form action="/" name="form1">
  <table>...</table>
</form>

但更好的是:如果可能的话,建立没有表的表格。

You can't. Your only option is to divide this into multiple tables and put the form tag outside of it. You could end up nesting your tables, but this is not recommended:

<table>
  <tr><td>
  <form>
    <table><tr><td>id</td><td>name</td>...</tr></table>
  </form>
</td></tr>
</table>

I would remove the tables entirely and replace it with styled html elements like divs and spans.

 
3 
Agreed, don't use tables, use labels and form elements and CSS to align them. See websiteoptimization.com/speed/tweak/forms – Dan Diplo Aug 8 '09 at 19:41
   
i was afraid that it wouldn't be possible... grrr. Thanks though – Ropstah Aug 8 '09 at 19:52
35 
Please, don't be "tablephobic" by default. There are plenty of cases where data must be displayed in table form that also may require a form in it. I'm currently facing one of these cases. In the end, you end up mimicking table syntax with different tags... – Pere Jul 1 '14 at 14:22

I had a similar question and answer this answer in question HTML: table of forms? solved it for me. (Not sure if it is XHTML, but it works in an HTML5 browser).

You can use css to give table layout to other elements. Although it is only tested on chrome.

.table { display: table; } 
.table>* { display: table-row; }
.table>*>* { display: table-cell; }

Then you use the following valid html.

<div class="table"> 
    <form>
        <div>snake<input type="hidden" name="cartitem" value="55"></div>
        <div><input name="count" value=4/></div>
    </form>
</div>
 
1 
This gets my vote - very clean solution (and verified working)! – kwhitley Oct 31 '13 at 2:15

It's worth mentioning that this is possible in HTML5, using the "form" attribute for input elements:

<table>
    <tr>
        <td>Id</td>
        <td>Name</td>
        <td>Description</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td><form id="form1"><input type="hidden" name="id" value="1" /></form></td>
        <td><input form="form1" type="text" name="name" value="Name" /></td>
        <td><input form="form1" type="text" name="description" value="Description" /></td>
        <td><input form="form1" type="submit" value="Save" /></td>
    </tr>
    <tr>
        <td><form id="form2"><input type="hidden" name="id" value="1" /></form></td>
        <td><input form="form2" type="text" name="name" value="Name" /></td>
        <td><input form="form2" type="text" name="description" value="Description" /></td>
        <td><input form="form2" type="submit" value="Save" /></td>
    </tr>
</table>

While clean in its lack of JS and use of original elements, unfortunately this isn't working in IE10.

shareimprove this answer
 
4 
"isn't working in IE" - classic – crmpicco Jan 22 '16 at 14:50
   
Unfortunately this is neither supported by IE nor Edge: caniuse.com/#feat=form-attribute – Johannes RudolphFeb 18 at 15:45

You just have to put the <form ... > tag before the <table> tag and the </form> at the end.

Hopte it helps.

shareimprove this answer
 
2 
this won't work as there are form elements with the same name for every tablerow... – Ropstah Aug 8 '09 at 19:51
2 
Actually it would work fine - append the row number to each item, e.g. 'r1_name', then on the next row 'r2_name', it's pretty easy to parse() these on the server – monk.e.boy Mar 8 '11 at 9:42
4 
It will work fine in many cases, but if your table has 1000 rows, you probably don't want to submit the entire thing when all you want to do is update a single row. – Mike Jul 13 '14 at 23:31

I'd say you can, although it doesn't validate and Firefox will re-arrange the code (so what you see in 'View generated source' when using Web Developer may well surprise). I'm no expert, but putting

<form action="someexecpage.php" method="post">

just ahead of the

<tr>

and then using

</tr></form>

at the end of the row certainly gives the functionality (tested in Firefox, Chrome and IE7-9). Working for me, even if the number of validation errors it produced was a new personal best/worst! No problems seen as a consequence, and I have a fairly heavily styled table. I guess you may have a dynamically produced table, as I do, which is why parsing the table rows is a bit non-obvious for us mortals. So basically, open the form at the beginning of the row and close it just after the end of the row.

shareimprove this answer
 

If you try to add a form warping a tr element like this

<table>
<form>
<tr>
<td><input type="text"/></td>
<td><input type="submit"/></td>
</tr>
</form>
</table>

some browsers in the process of rendering will close form tag right after the declaration leaving inputs outside of the element

something like this

<table>
    <form></form>
    <tr>
    <td><input type="text"/></td>
    <td><input type="submit"/></td>
    </tr>
    </table>

This issue is still valid for warping multiple table cells

As stereoscott said above, nesting tables are a possible solution which is not recommended. Avoid using tables.

shareimprove this answer
 

In fact I have the problem with a form on each row of a table, with javascript (actually jquery) :

like Lothre1 said, "some browsers in the process of rendering will close form tag right after the declaration leaving inputs outside of the element"

which makes my input fields OUTSIDE the form, therefore I can't access the children of my form through the DOM with JAVASCRIPT..

typically, the following JQUERY code won't work :

$('#id_form :input').each(function(){/*action*/});
// this is supposed to select all inputS 
// within the form that has an id ='id_form'

BUT the above exemple doesn't work with the rendered HTML :

<table>
    <form id="id_form"></form>
    <tr id="tr_id">
    <td><input type="text"/></td>
    <td><input type="submit"/></td>
    </tr>
    </table>

I'm still looking for a clean solution (though using the TR 'id' parameter to walk the DOM would fix this specific problem)

dirty solution would be (for jquery):

$('#tr_id :input').each(function(){/*action*/});
// this will select all the inputS
// fields within the TR with the id='tr_id'

the above exemple will work, but it's not really "clean", because it refers to the TR instead of the FORM, AND it requires AJAX ...

EDIT : complete process with jquery/ajax would be :

//init data string
// the dummy init value (1=1)is just here 
// to avoid dealing with trailing &
// and should not be implemented
// (though it works)
var data_str = '1=1'; 
// for each input in the TR
$('#tr_id :input').each(function(){

 //retrieve field name and value from the DOM
 var field = $(this).attr('name');
 var value = $(this).val();

 //iterate the string to pass the datas
 // so in the end it will render s/g like
 // "1=1&field1_name=value1&field2_name=value2"...
 data_str += '&' + field + '=' + value; 


});

//Sendind fields datawith ajax 
// to be treated 
$.ajax({
 type:"POST",
 url: "target_for_the_form_treatment",
 data:data_string,
 success:function(msg){
    /*actions on success of the request*/
 });
});

this way, the "target_for_the_form_treatment" should receive POST data as if a form was sent to him (appart from the post[1] = 1, but to implement this solution i would recommand dealing with the trailing '&' of the data_str instead).

still I don't like this solution, but I'm forced to use TABLE structure because of the dataTables jquery plugin...

shareimprove this answer
 
1 
This is more of a comment than an answer... especially because you don't explain how $('#tr_id :input').each(function(){/*action*/}); solves the problem. – Ben D Oct 11 '12 at 17:13
   
well i've edited the message to detail the different protions of jquery, but i think this is more than a comment : javascript is one of the reason why even though POST data will be sent to the server, DOM hierarchy is not respected ... – renard Oct 11 '12 at 22:45
   
You've solved the question of how to access the inputs through Javascript, but that was never the problem in the first place... the problem is that the OP wants each row of a table to be a submit-able form. Your post does not answer that question. You could convert it into an answer by showing how to use JQuery/javascript to submit discrete blocks of the table (by, say, having the submit button on each row grab the values from the elements on it's parent tr and then pass these values into an AJAX post). There are other ways to do it, you need to show how it addresses the OP's problem – Ben D Oct 11 '12 at 23:08
   
you got a point, i'm editnig my post – renard Oct 12 '12 at 12:59

Im late to the party, but this worked great for me and the code should explain itself;

<script type="text/javascript">
    function formAJAX(btn){
        var $form = $(btn).closest('[action]');
        var str = $form.find('[name]').serialize();
        $.post($form.attr('action'), str, function(data){
            //do stuff
        });
    }
<script>

HTML:

<tr action="scriptURL.php">
    <td>
        Field 1:<input type="text" name="field1"/>
    </td>
    <td>
        Field 2:<input type="text" name="field2" />
    </td>
    <td><button type="button" onclick="formAJAX(this)">Update</button></td>
</tr>
shareimprove this answer
 
   
@pbalaga: See edit history, but since it's been edited, the comment was indeed obsolete, so I deleted it. – t0mppa Apr 23 '14 at 12:33

来自  https://stackoverflow.com/questions/1249688/html-is-it-possible-to-have-a-form-tag-in-each-table-row...


普通分类: