【问题标题】:Add required Checkbox to a custom button [closed]将所需的复选框添加到自定义按钮 [关闭]
【发布时间】:2020-03-09 20:34:36
【问题描述】:

我想在我的页面上添加“我接受条款和条件”复选框。

我试过了

<p><input type="checkbox" required>
<input type="submit" name="submit" value="submit" onclick="if(!this.form.checkbox.checked){alert('You must agree to the terms first.');return false}"  />

但这种类型的复选框链接到此提交按钮,而不是链接到我在该页面上已有的表单提交按钮。这是我希望它附加到的按钮代码

<a class="wt-btn wt-post-service" data-id="" data-type="add" href="javascript:;"><?php esc_html_e('Save &amp; Update',); ?></a>

请帮忙。

谢谢。

【问题讨论】:

    标签: javascript php jquery html css


    【解决方案1】:

    当您使用 type=submit 或 name=submit 时,行为由提交按钮控制。只需阻止默认行为,选中该框,然后发出警报或提交表单。

    function warning(){
       event.preventDefault();
    
       var check = document.getElementById('checkbox');
       var myForm = document.getElementById('myForm');
    
       if (check.checked==false){
         alert("you must agree to terms and conditions");
       }else{
         myForm.submit();
       }
    }
    <form id='myForm' action="/action_page.php" method = 'POST'>
    <p><input id='checkbox' type="checkbox" required>
    <input type="button" name='button' value="submit" onclick="warning()"  />
    </form>
    
    <a class="wt-btn wt-post-service" data-id="" data-type="add" href="javascript:;"><?php esc_html_e('Save &amp; Update',); ?></a>
    Please help.

    【讨论】:

    • 我想消除 并想使用我已经拥有的按钮 这个
    【解决方案2】:

    我认为您正在使用 Javascript/jquery 提交表单。因此,您需要在单击按钮时使用 Jquery 检查是否选中了复选框。

    $(document).ready(function(){
        //Submit button click
                $('.wt-post-service').click(function(){
        if($(input[type="checkbox"]).is(":not(:checked)")){
                        alert("You must agree to the terms first.");
                    }
        });
        });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-15
    • 2019-12-17
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    相关资源
    最近更新 更多