使用Jquery将Checkbox值添加到Field

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个回答

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>
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问