Exclude hidden form fields from submit

后端 未结 2 2231
太阳男子
太阳男子 2021-02-19 05:50

I\'m hiding/showing a div based on the state of a checkbox.



        
2条回答
  •  暖寄归人
    2021-02-19 06:26

    You can add a disabled attribute to any fields you don't want to submit.

    function toggle_form_element(id) {
        if ((document.getElementsByName(id)[0].checked)) {
            document.getElementById('sometimesHidden').setAttribute("disabled", "disabled");
        } else {
            document.getElementById('sometimesHidden').removeAttribute("disabled");
        }
    }
    

    For a jQuery solution:

    // Disables all input, select & textarea tags within the .sometimesHidden div
    if(checked) {
      $("input, select, textarea", $(".sometimesHidden")).attr("disabled", "disabled");
    }
    else {
      $("input, select, textarea", $(".sometimesHidden")).removeAttr("disabled");
    }
    

提交回复
热议问题