如果未选中复选框,请不要使用提交输入type = image发送表单

时间:2017-03-28 16:55:45

标签: javascript jquery forms

如果未选中此复选框,我需要禁用此表单的提交(最好在点击时发送带有javascript的警报消息)。

我找到了许多经典提交按钮的解决方案,但它们都不适用于提交图像。我一直都能显示错误信息但仍然正在发送表格。

你知道我该如何解决这个问题?谢谢

<form method="POST" name="paypal" action="">
<input type="checkbox" name="checkbox" id="checkbox"/> 
<label for="checkbox" class="agree">I accept the</label> <a href="#" target="_blank">conditions</a>
<input type="image" name="submit" src="http://www.example.com/paypal.png">
</form>

3 个答案:

答案 0 :(得分:1)

<form method="POST" name="paypal" action="" onsubmit="return (function() { if (!document.getElementById('checkbox').checked) { alert('You must accept the conditions');return false; }})()">
<input type="checkbox" name="checkbox" id="checkbox"/> 
<label for="checkbox" class="agree">I accept the</label> <a href="#" target="_blank">conditions</a>
<input type="image" name="submit" src="http://www.example.com/paypal.png">
</form>

我添加了一个onsubmit事件处理程序,它运行一个函数来确定是否选中了复选框,如果不是,我们会显示一个警告并返回false以防止提交表单。

答案 1 :(得分:0)

只需听取表单上的提交事件,检查所有必要的内容,如果表单正确则返回true,否则返回false。如果您返回false,则不应提交表单。

Service

答案 2 :(得分:0)

单击图像时,检查是否选中了复选框。

jQuery('input[name="submit"]').on('click', function(){
      var accept = jQuery('input[name="checkbox"]').is(":checked");
      if(accept == false){
        alert("you did not accept the terms");
        return false;
      }
      if(accept == true){
        //submit form
      }
    });
相关问题