0

我真的很难弄清楚如何找到所有 Checkbox 输入 ID,并只用一个字符串或可以处理请求的东西将它们单独传递给我的 SQL DB。这就是我到目前为止所拥有的,它是重复的和多余的,并且是一个 PAAAIN,因为我还有 70 个复选框要通过,这变得越来越荒谬了。当然有一种方法可以在 Javascript 中做到这一点?目前这一切都有效,我有一个单独的 PHP 上传脚本,可以正确地将所有这些信息发送到数据库,我只想知道一个快捷方式或数组或任何帮助我管理我的 70 多个复选框的东西选择并且需要发送到数据库。

下面的代码是我正在使用的,它可以工作,但正如你所见,处理所有这些 ID 变得荒谬......有什么想法吗?我将永远感激不尽。

Separate snippet form HTML file:
<tr>
                    <td>
                        <input id="fire_extinguisher" value="fire_extinguisher" type="checkbox">Fire Extinguisher
                    </td>
                    <td>
                        <input id="eye_wash_shower" type="checkbox">Eye Wash/Shower
                    </td>
                    <td>
                        <input id="ladder_scaffold" type="checkbox">Ladder/Scaffold
                    </td>
                    <td>
                        <input id="warning_signs" type="checkbox">Warning Signs
                    </td>
                    <td>
                        <input id="communications" type="checkbox">Communications
                    </td>
                </tr>

JS文件

//UPLOAD DATA
//Global variables
var jsaform = {
    'location_detail' : "",
    'emergency_number' : "",
    'today' : "",
    'person_in_charge' : "",
    'other_employees' : "",
    'job_description' : "",
    'job_hazards' : "",
    'energy_source_controls' : "",
    'fire_extinguisher' : "No",
    'eye_wash_shower' : "No",
    'ladder_scaffold' : "No",
    'warning_signs' : "No",
    'communications' : "No",
    'machine_guarding' : "No",
    'forklift' : "No",
    'transportation' : "No",
    'welding_cutting' : "No",
    'test_equipment' : "No",
    'barricades' : "No",
    'gfci_cord' : "No",
    'protective_covering' : "No",
    'hand_tools' : "No",
    'ventilation' : "No",
    'msds' : "No",
    'power_tools' : "No",
    'soldering' : "No",
    'equipment_other' : "",
    'natural_fiber' : "No",
    'flame_resistant' : "No",
    'long_sleeves' : "No",
    'kevlar_sleeves' : "No",
    'tyvek' : "No",
    'hard_hat' : "No",
    'hearing_protection' : "No",
    'sun_screen' : "No",
    'safety_glasses' : "No",
    'goggles' : "No",
    'face_shield' : "No",
    'harness' : "No",
    'attachment_point' : "No",
    'personal_flotation_device' : "No",
    'full_face' : "No",
    'half_face' : "No",
    'dust_mask' : "No",
    'safety_shoes' : "No",
    'rubber_boots' : "No",
    'dielectric' : "No",
    'cotton' : "No",
    'leather' : "No",
    'hot_gloves' : "No",
    'protection_other' : ""
}

function uploaddata() { 

//Read all of the data from the page
jsaform.location_detail = document.getElementById("location_detail").value;
jsaform.emergency_number = document.getElementById("emergency_number").value;
jsaform.today = document.getElementById("today").value;
jsaform.person_in_charge = document.getElementById("person_in_charge").value;
jsaform.other_employees = document.getElementById("other_employees").value;
jsaform.job_description = document.getElementById("job_description").value;
jsaform.job_hazards = document.getElementById("job_hazards").value;
jsaform.energy_source_controls = document.getElementById("energy_source_controls").value;
jsaform.equipment_other = document.getElementById("equipment_other").value;
jsaform.protection_other = document.getElementById("protection_other").value;   
//Read Checked items
if (document.getElementById('fire_extinguisher').checked) {
                    jsaform.fire_extinguisher = 'Yes';
            }
if (document.getElementById('eye_wash_shower').checked) {
                    jsaform.eye_wash_shower = 'Yes';
            }
if (document.getElementById('ladder_scaffold').checked) {
                    jsaform.ladder_scaffold = 'Yes';
            }
if (document.getElementById('warning_signs').checked) {
                    jsaform.warning_signs = 'Yes';
            }

//Send to database upload function
upload_jsaform();   
}
    function upload_jsaform() {
        $.ajax({
      type: 'POST',
      url: './php/upload_jsaform.php',
      data: jsaform,
      async: false,
      dataType: 'text',
      success: function() {
        alert("Thank you. Your Job Safety Analysis form has been submitted.");
        },

      error: function(jqXHR, textStatus, errorThrown) {
        alert("Error... " + textStatus + "\n" + errorThrown);
            }
    });
    return false;
};
4

1 回答 1

0

You can loop like this:

for (eID in jsaform) {
    if(document.getElementById(eID) && document.getElementById(eID).checked) {
        jsaform[eID] = 'Yes';
    }
}
于 2013-02-02T21:35:19.000 回答