簡體   English   中英

如何捕獲復選框是否已被選中?

[英]How to capture whether a checkbox has been checked or not?

我有一個用於網絡研討會注冊的 Bootstrap 表單模式。 我已經刪除了其他字段,但我有多個文本字段被捕獲並推送到 CMS。

只是這 3 個復選框我似乎無法正確推送數據。 如果表單提交一次,復選框將全部記錄為“on”,但如果表單第二次提交,則選中/未選中的復選框將相應地推送“on”和“off”。 所以它適用於第二次提交,但不適用於第一次提交。 任何幫助將不勝感激。 我是 javascript 和 jquery 的新手,所以我現在正在猜測。

 $(document).ready(function() { $('.register-webinar').click(function() { checked = $("input[type=checkbox]:checked").length; if (!checked) { alert("You must select at least one session."); return false; } }); $('input.webinar').change(function() { if ($(this).is(":checked")) { $(this).val("on") } else { $(this).val("off") } }); $('#railway_form').on("submit", function(e) { e.preventDefault(); setTimeout(AoProcessForm(this), 200); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css"> <div class="form-popup-bg"> <div class="form-container"> <button id="btnCloseForm" class="close-button">X</button> <h1 style="text-align:center;">Register your attendance</h1> <p style="text-align:center;">Please select which session/s you wish to attend.</p> <form name="railway_webinar" id="railway_form" class="railway_webinar" action=""> <div class="form-group"> <input type="checkbox" id="Webinar_1" name="Webinar_1" class="webinar"> <label for="Webinar_1">webinar 1</label><br/> <input type="checkbox" id="Webinar_2" name="Webinar_2" class="webinar"> <label for="Webinar_2">webinar 2</label><br/> <input type="checkbox" id="Webinar_3" name="Webinar_3" class="webinar"> <label for="Webinar_3">webinar 3</label> </div> <input type="submit" value="Reserve my seat!" class="form-block-cta register-webinar" /> </form> </div> </div>

弄清楚了。 我需要做的就是將復選框值設置為“關閉”,因為默認情況下它們都將“打開”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM