【问题标题】:Checkbox Dynamic jquery when adding a new row添加新行时的复选框动态jquery
【发布时间】:2016-08-23 12:43:37
【问题描述】:

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

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

该字段名为 add_overtime_hours

<!-- 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>

感谢您对我做错的任何帮助。

谢谢你

【问题讨论】:

    标签: php jquery checkbox


    【解决方案1】:

    您的代码中有一些错误。

    我修复了它们,所以最终的代码在下面的 sn-p 中。

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

    name="add_finish[1]"
    name="add_finish[2]"
    ......
    

    当然,每个名称根据字段名称有不同的前缀。

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

    var rownumhours = 0;
    
    function addhours(obj, e) {
      rownumhours++;
      var hoursrow = '<p id="rownumhours' + rownumhours + '">Start Time: <input type="time" name="add_start[' + rownumhours + ']" size="4" value="' +
          $(obj).closest('span.headings').find('[name="add_start"]').val() + '">  Finish Time: <input type="time" name="add_finish[' + rownumhours + ']" value="' +
          $(obj).closest('span.headings').find('[name="add_finish"]').val() + '"> Date: <input type="date" name="add_date[' + rownumhours + ']" value="' +
          $(obj).closest('span.headings').find('[name="add_date"]').val() + '"> Overtime: <input type="checkbox" name="add_overtime_hours[' + rownumhours + ']" size="1" value="' +
          $(obj).closest('span.headings').find('[name="add_overtime_hours"]').val() + '"' +
          (($(obj).closest('span.headings').find('[name="add_overtime_hours"]').is(':checked')) ? 'checked' : '') +
          ' "> <input type="button" value="Remove" onclick="removeRow(' +
          rownumhours + ');"></p>';
      jQuery('#hoursrow').append(hoursrow);
    }
    
    function removeRow(rnum) {
      jQuery('#rownumhours' + rnum).remove();
    }
    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    
    <table>
        <tbody>
        <tr>
            <td colspan="3" align="left">
                <div id="hoursrow">
                    <td>&nbsp;</td>
                    <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, event);" 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>
        </tbody>
    </table>

    如何获取所有 add_overtime_hours 的示例如下:

    $myboxes = $_POST['add_overtime_hours'];
    if(empty($myboxes)) {
         echo("You didn't select any boxes.");
    } else {
        $i = count($myboxes);
        echo("You selected $i box(es): ");
        for($j=0; $j < $i; $j++) {
             echo($myboxes[$i] . " ");
        }
    }
    
          Show_Overtime: <input type="text" name="add_overtime_hours[]" size="1" value="' + (($(obj).closest('span.headings').find('[name="add_overtime_hours"]').is(':checked')) ? 'checked' : '1' )' ">
    

    【讨论】:

    • 您好 GaetanoM 感谢您查看我的代码。我已经在主页上更改了它,尽管加班时间似乎仍然没有通过。同样出于某种原因,我在第一行输入的日期是 23/08/2016,当它传播到新行时,它显示 2016-08-23。
    • 你好 gaetanoM 非常感谢你,我自己也不会得到这个。我现在将去了解 .closest 之前的 div 然后保存到数据库中,我需要从更改中更改现有代码上的任何内容。我假设它仍在创建相同的名称来填充数据库。我还想将复选框插入到数据库值中,这也可能吗?非常感谢。
    • 非常感谢,我一直在尝试将复选框的值复制到 0 或 1,具体取决于复选框是否被选中。我曾尝试在新文本框中使用 .val,但我不能完全正确
    • @RobertGrain 对不起,但根据 SO,一问一答。我们不能永远继续同一个问题。您只能接受/否决。这取决于你。
    • 您好 Gaetano 很抱歉您确实帮了我很多忙,我肯定会接受答案,因为原始问题运行良好。我将继续努力,感谢您给了我一个良好的开端。
    【解决方案2】:

    谢谢 gaetanoM 这是一个很好的解决方案。我现在必须经历这一切才能理解它。 谢谢你

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      • 2017-06-11
      • 1970-01-01
      相关资源
      最近更新 更多