如何从HMTL5表中获取检查输入

时间:2016-06-01 20:28:15

标签: php jquery html5 radio-button html-table

我想知道是否有人知道如何获取单选按钮的选中值。有5列和几行,用户每列只能选择一个单选按钮。

我一直在尝试获取值,但我无法通过JQuery获取它,或通过post发送到PHP文件。

<table class="table table-striped table-hover " id='tableIncoming'>
    <thead>
        <tr>
            <th>#</th>
            <th>Route</th>
            <th>Monday</th>
            <th>Tuesday</th>
            <th>Wednesday</th>
            <th>Thursday</th>
            <th>Friday</th>
        </tr>
    </thead>
    <tbody>
        {section name=lindex loop=$INCOMING_LIST}       
        <tr style="" class="{if $INCOMING_LIST[lindex].name eq 'Tres Rios' || $INCOMING_LIST[lindex].name eq 'Cartago'}info{/if}" id="tr{$INCOMING_LIST[lindex].name}-{$INCOMING_LIST[lindex].time}"> 
            <td>{$smarty.section.lindex.index +1}</td>
            <td style="width:220px;"> {$INCOMING_LIST[lindex].name}, {$INCOMING_LIST[lindex].time} </td>  
            <td><input name="M" type="radio"></td>
            <td><input name="T" type="radio"></td>
            <td><input name="W" type="radio"></td>
            <td><input name="Th" type="radio"></td>
            <td><input name="F" type="radio"></td>
        </tr> 
        {/section}
        <tr style="" class="" id='trWNUI'> 
            <td>-</td>
            <td  style="width:220px;" name='wnui'>Will not use</td>  
            <td><input name="M" type="radio"></td>
            <td><input name="T" type="radio"></td>
            <td><input name="W" type="radio"></td>
            <td><input name="Th" type="radio"></td>
            <td><input name="F" type="radio"></td>
        </tr>
    </tbody>
</table> 

Table 感谢@Lennon,我更新了我的表以提供值,这是代码现在的样子:

{section name=lindex loop=$INCOMING_LIST}       
    <tr style="" class="{if $INCOMING_LIST[lindex].name eq 'Tres Rios' || $INCOMING_LIST[lindex].name eq 'Cartago'}info{/if}" id="tr{$INCOMING_LIST[lindex].name}-{$INCOMING_LIST[lindex].time}"> 
        <td>{$smarty.section.lindex.index +1}</td>
        <td style="width:220px;"> {$INCOMING_LIST[lindex].name}, {$INCOMING_LIST[lindex].time} </td>  
        <td><input name="Mi[]" value='I, M, {$INCOMING_LIST[lindex].name}, {$INCOMING_LIST[lindex].time}' type="radio"></td>
        <td><input name="Ti[]" value='I, T, {$INCOMING_LIST[lindex].name}, {$INCOMING_LIST[lindex].time}' type="radio"></td>
        <td><input name="Wi[]" value='I, W, {$INCOMING_LIST[lindex].name}, {$INCOMING_LIST[lindex].time}' type="radio"></td>
        <td><input name="Thi[]" value='I, Th, {$INCOMING_LIST[lindex].name}, {$INCOMING_LIST[lindex].time}' type="radio"></td>
        <td><input name="Fi[]" value='I, F, {$INCOMING_LIST[lindex].name}, {$INCOMING_LIST[lindex].time}' type="radio"></td>
    </tr> 
{/section}

2 个答案:

答案 0 :(得分:0)

您必须在单选按钮的名称上使用括号。 因此,当您提交它们时,PHP会将其识别为数组。

此处示例:Multiple radio button array for php form

此:

<tr style="" class="{if $INCOMING_LIST[lindex].name eq 'Tres Rios' || $INCOMING_LIST[lindex].name eq 'Cartago'}info{/if}" id="tr{$INCOMING_LIST[lindex].name}-{$INCOMING_LIST[lindex].time}"> 
            <td>{$smarty.section.lindex.index +1}</td>
            <td style="width:220px;"> {$INCOMING_LIST[lindex].name}, {$INCOMING_LIST[lindex].time} </td>  
            <td><input name="M" type="radio"></td>
            <td><input name="T" type="radio"></td>
            <td><input name="W" type="radio"></td>
            <td><input name="Th" type="radio"></td>
            <td><input name="F" type="radio"></td>
        </tr>

需要这样:

<tr style="" class="{if $INCOMING_LIST[lindex].name eq 'Tres Rios' || $INCOMING_LIST[lindex].name eq 'Cartago'}info{/if}" id="tr{$INCOMING_LIST[lindex].name}-{$INCOMING_LIST[lindex].time}"> 
            <td>{$smarty.section.lindex.index +1}</td>
            <td style="width:220px;"> {$INCOMING_LIST[lindex].name}, {$INCOMING_LIST[lindex].time} </td>  
            <td><input name="M[]" type="radio"></td>
            <td><input name="T[]" type="radio"></td>
            <td><input name="W[]" type="radio"></td>
            <td><input name="Th[]" type="radio"></td>
            <td><input name="F[]" type="radio"></td>
        </tr> 

您可以通过$ _POST ['m'] ['ID_OF_CHECKED_ITEM']

进行访问

答案 1 :(得分:0)

你所要做的就是给同一行的单选按钮提供相同的名称&#34;

例如:

<tr style="" class="{if $INCOMING_LIST[lindex].name eq 'Tres Rios' || $INCOMING_LIST[lindex].name eq 'Cartago'}info{/if}" id="tr{$INCOMING_LIST[lindex].name}-{$INCOMING_LIST[lindex].time}"> 
            <td>{$smarty.section.lindex.index +1}</td>
            <td style="width:220px;"> {$INCOMING_LIST[lindex].name}, {$INCOMING_LIST[lindex].time} </td>  
            <td><input name="M" type="radio"></td>
            <td><input name="T" type="radio"></td>
            <td><input name="W" type="radio"></td>
            <td><input name="Th" type="radio"></td>
            <td><input name="F" type="radio"></td>
        </tr> 

应更改为:

<tr style="" class="{if $INCOMING_LIST[lindex].name eq 'Tres Rios' || $INCOMING_LIST[lindex].name eq 'Cartago'}info{/if}" id="tr{$INCOMING_LIST[lindex].name}-{$INCOMING_LIST[lindex].time}"> 
            <td>{$smarty.section.lindex.index +1}</td>
            <td style="width:220px;"> {$INCOMING_LIST[lindex].name}, {$INCOMING_LIST[lindex].time} </td>  
            <td><input name="row1" value="M" type="radio"></td>
            <td><input name="row1" value="T" type="radio"></td>
            <td><input name="row1" value="W" type="radio"></td>
            <td><input name="row1" value="Th" type="radio"></td>
            <td><input name="row1" value="F" type="radio"></td>
        </tr> 

您可以在jquery或php中以$('row1').val()

的形式访问$_POST['row1']的值