Php 添加新行时选中Dynamic jquery

Php 添加新行时选中Dynamic jquery,php,jquery,checkbox,Php,Jquery,Checkbox,我对使用jquery和php的复选框有问题。我正在添加小时数行,我想在表单上添加一个加班复选框,但当我使用jquery将数据添加到新行时,允许在我无法添加复选框后添加多行以添加到数据库中,我的表单数据如下所示 我想添加复选框(已选中/未选中)以及0未选中和1(如果选中)的值 该字段称为“添加加班小时” <!-- this ext section is the Adding Hours on the jobsheet --> <script type="text/javascri

我对使用jquery和php的复选框有问题。我正在添加小时数行,我想在表单上添加一个加班复选框,但当我使用jquery将数据添加到新行时,允许在我无法添加复选框后添加多行以添加到数据库中,我的表单数据如下所示

我想添加复选框(已选中/未选中)以及0未选中和1(如果选中)的值

该字段称为“添加加班小时”

<!-- this ext section is the Adding Hours on the jobsheet -->
<script type="text/javascript">
var rownumhours = 0;
function addhours(frm) {
rownumhours ++;
<!-- the next part creates the html to add dynamic fields into the Div hoursrow
var hoursrow = '<p id="rownumhours'+rownumhours+'">Start Time: <input type="text" name="add_start[]" size="4" value="'+frm.add_start.value+'">  Finish Time: <input type="text" name="add_finish[]" value="'+frm.add_finish.value+'"> Date: <input type="text" name="add_date[]" value="'+frm.add_date.value+'"> Overtime: <input type="checkbox" name="add_overtime_hours[]" size="1" value="'+frm.add_overtime_hours.value+' "> <input type="button" value="Remove" onclick="removeRow('+rownumhours+');"></p>';
<!-- the next part looks at the partsrow div lower in the page and then appends the values to the row variable above -->
jQuery('#hoursrow').append(hoursrow);
frm.add_start.value = '';
frm.add_finish.value = '';
frm.add_overtime_hours.value = 'N';
frm.add_date.value = '';
}

function removeRow(rnum) {
    jQuery('#rownumhours'+rnum).remove();
}
</script>

<tr>
<td colspan="3" align="left">
    <div id="hoursrow">
        <p>
            <span class="text">Hours Labour :</span></span>
        </p>
        <p>
            <span class="headings"><span class="text"><span class="hours">Start Time:
            <input type="time" name="add_start" size="4" />Finish Time:
            <input type="time" name="add_finish" size="4" />Date:
            <input type="date" name="add_date" size="4" />OVT:
            <input type="checkbox" name="add_overtime_hours" id="add_overtime_hours"  Value="1" size="1" maxlength="1" />
            </span></span>
            <span class="text"><span class="hours">
                <input onclick="addhours(this.form);" type="button" value="Add Labour" />
                </span></span>
        </p>
        <p class="headings"><span class="text"><span class="hours">
          <span class="info">(This row will not be saved unless you click on "Add Labour" first) </span></span></span>
        </p>
    </div>
</td>
<td width="7"></td>
</tr>
<tr>
    <td>&nbsp;</td>
</tr>

var rownumhours=0;
功能添加小时数(frm){
rownumhours++;
jQuery(“#hoursrow”).append(hoursrow);
frm.add_start.value='';
frm.add_finish.value='';
frm.add_加班_小时数.value='N';
frm.add_date.value='';
}
函数移除器(rnum){
jQuery('#rownumhours'+rnum).remove();
}

工时:

开始时间: 完成时间: 日期: OVT:

(除非先单击“添加人工”,否则不会保存此行)

谢谢你对我所做错事的帮助


谢谢

您的代码中有一些错误

我修复了它们,所以最后的代码在下面的代码片段中

为了区分每个字段的名称,我使用了以下标准:

name="add_finish[1]"
name="add_finish[2]"
......
当然,根据字段的名称,每个名称都有不同的前缀

因此,您可以使用服务器端组织的名称

var rownumhours=0;
功能添加小时数(obj,e){
rownumhours++;

var hoursrow='

开始时间:完成时间:日期:加班时间:谢谢gaetanoM,这是一个很好的解决方案。我现在必须通读一遍才能理解它。


谢谢你

你好GaetanoM谢谢你查看我的代码。我在主页上更改了代码,尽管加班时间似乎还没有结束。另外,出于某种原因,我在第一行上输入的日期是2016年8月23日,当它发布到新行时,显示的是2016-08-23。你好GaetanoM非常感谢你,我不会这么做我自己从来没有得到过。我现在将去了解。最近的div然后保存到数据库中。我需要从更改中更改现有代码的任何内容。我假设它仍在创建相同的名称来填充数据库。我还想在数据库值中插入复选框。这可能是t吗oo?非常感谢。非常感谢。我一直在尝试将复选框的值复制到0或1中,具体取决于复选框是否选中。我尝试在新文本框中使用.val,但我无法完全获得它right@RobertGrain对不起,根据SO,一个问题一个答案。我们不能继续同一个问题永远。你只能接受或不接受。这取决于你。你好,盖塔诺,我很抱歉,你确实帮了我很多,我当然会接受答案,因为原来的问题很好。我会继续做剩下的工作,谢谢你给了我一个很好的开始。