duanduo3712 2016-11-03 15:24
浏览 68
已采纳

使用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条回答 默认 最新

  • dongwupu5991 2017-01-18 19:24
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?