使用Jquery将复选框vaue添加到字段(Using Jquery to add a Checkbox vaue to a Field)

您好我试图在我的数组中添加一个复选框的结果,以便在提交时添加到数据库中。

另一个用户好心地改变了我的原始代码,但我不知道如何将我的复选框的结果添加到我的数组。

它现在显示在屏幕上,但我不能将它复制到一个字段,以便能够将字段保存到我的数据库,以便当我再次打开页面时,它将从数据库加载,并显示是否超时在我的记录小时数。

我想保存一个值为checked = Y,当没有检查= N到数组时,我想我可能需要将值复制到隐藏字段,但我无法让它工作。

<!-- this next section is to dynamically add the parts fields to the jobsheet page so more parts can be added on the page without it starting out too large. --> <!-- this ext section is the Adding Hours on the jobsheet --> 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() + '"' + Overtime: <input type="checkbox" name="add_overtime_hours[' + rownumhours + ']" size="1" value="' + (($(obj).closest('span.headings').find('[name="add_overtime_hours"]').is(':checked')) ? 'checked' : '1') + $(obj).closest('span.headings').find('[name="add_overtime_hours"]').val() + '"> <input type="button" value="Remove" onclick="removeRow(' + rownumhours + ');"></p>'; jQuery('#hoursrow').append(hoursrow); } function removeRow(rnum) { jQuery('#rownumhours' + rnum).remove(); } </script>

Hello I am trying to add a result of a checkbox into my array to add to a database when submitted.

Another user kindly changed my original code but I can't work out how to add the result of my checkbox to my array.

It now shows on the screen properly but I cant get it to copy to a field to be able to save the field to my database so that when I open the page again it will then load from the database and show if it is overtime on my hours recorded.

I want to save a Value of checked = Y and when not checked = N to the array I think I might need to copy the value to a hidden field but I can't get it to work.

<!-- this next section is to dynamically add the parts fields to the jobsheet page so more parts can be added on the page without it starting out too large. --> <!-- this ext section is the Adding Hours on the jobsheet --> 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() + '"' + Overtime: <input type="checkbox" name="add_overtime_hours[' + rownumhours + ']" size="1" value="' + (($(obj).closest('span.headings').find('[name="add_overtime_hours"]').is(':checked')) ? 'checked' : '1') + $(obj).closest('span.headings').find('[name="add_overtime_hours"]').val() + '"> <input type="button" value="Remove" onclick="removeRow(' + rownumhours + ');"></p>'; jQuery('#hoursrow').append(hoursrow); } function removeRow(rnum) { jQuery('#rownumhours' + rnum).remove(); } </script>

最满意答案

你好,我终于完成了解决我的问题。

我已经通过显示复选框将作业拆分出来,然后还有一个隐藏的文本框,其中包含值为1或0的相同名称,然后这是我放入Mysql数据库的值。

var rownumhours = 0;

function addhours(obj, e) { var hoursrow = '<p id="rownumhours' + rownumhours + '">Start Time: <input type="time" name="add_start[]" size="4" value="' + $(obj).closest('span.headings').find('[name="add_start"]').val() + '"> Finish Time: <input type="time" name="add_finish[]" value="' + $(obj).closest('span.headings').find('[name="add_finish"]').val() + '"> Date: <input type="date" name="add_date[]" value="' + $(obj).closest('span.headings').find('[name="add_date"]').val() + '"> <input type="hidden" name="show_overtime_hours[]" size="1" value="' + (($(obj).closest('span.headings').find('[name="add_overtime_hours"]').is(':checked')) ? '1' : '0' ) + '"> Overtime: <input type="checkbox" name="add_overtime_hours[]" 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); rownumhours++; $(obj).closest('span.headings').find('[name="add_start"]').val(""); $(obj).closest('span.headings').find('[name="add_finish"]').val(""); $(obj).closest('span.headings').find('[name="add_date"]').val(""); $(obj).closest('span.headings').find('[name="show_overtime_hours"]').val(""); $(obj).closest('span.headings').find('[name="add_overtime_hours"]').removeAttr('checked'); } function removeRow(rnum) { jQuery('#rownumhours' + rnum).remove(); } </script>

Hello I finally finished the solution to my problem.

I have split the jobs out by showing the Checkbox and then also having a hidden text box of the same name that contains a value of either 1 or 0 and then this is the value i put into the Mysql Database.

var rownumhours = 0;

function addhours(obj, e) { var hoursrow = '<p id="rownumhours' + rownumhours + '">Start Time: <input type="time" name="add_start[]" size="4" value="' + $(obj).closest('span.headings').find('[name="add_start"]').val() + '"> Finish Time: <input type="time" name="add_finish[]" value="' + $(obj).closest('span.headings').find('[name="add_finish"]').val() + '"> Date: <input type="date" name="add_date[]" value="' + $(obj).closest('span.headings').find('[name="add_date"]').val() + '"> <input type="hidden" name="show_overtime_hours[]" size="1" value="' + (($(obj).closest('span.headings').find('[name="add_overtime_hours"]').is(':checked')) ? '1' : '0' ) + '"> Overtime: <input type="checkbox" name="add_overtime_hours[]" 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); rownumhours++; $(obj).closest('span.headings').find('[name="add_start"]').val(""); $(obj).closest('span.headings').find('[name="add_finish"]').val(""); $(obj).closest('span.headings').find('[name="add_date"]').val(""); $(obj).closest('span.headings').find('[name="show_overtime_hours"]').val(""); $(obj).closest('span.headings').find('[name="add_overtime_hours"]').removeAttr('checked'); } function removeRow(rnum) { jQuery('#rownumhours' + rnum).remove(); } </script>

更多推荐