Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript PHP仅在选中复选框时提交表单_Javascript_Checkbox - Fatal编程技术网

Javascript PHP仅在选中复选框时提交表单

Javascript PHP仅在选中复选框时提交表单,javascript,checkbox,Javascript,Checkbox,我有一个带有复选框和输入文本的表单。这是我的表格: 尝试完整的示例,它应该可以工作 然后更正代码 <!DOCTYPE html> <html> <head> <script> function validateForm() { var x = document.forms["myForm"]["fname"].value; if (x == null || x == "") { alert("Name must be

我有一个带有复选框和输入文本的表单。这是我的表格:


尝试完整的示例,它应该可以工作

然后更正代码

<!DOCTYPE html> <html> <head> <script> function validateForm() {
    var x = document.forms["myForm"]["fname"].value;
    if (x == null || x == "") {
        alert("Name must be filled out");
        return false;
    } } </script> </head> <body>

<form name="myForm" action="index.php" onsubmit="return validateForm()" method="post"> Name: <input type="text" name="fname"> <input type="submit" value="Submit"> </form>

</body> </html>
谢谢
Amit

代码中的上下文丢失了。尝试:

<script type=”text/javascript”>

 function foo(){
var checkbox = document.getElementById("chbx");
   if(checkbox.checked){
     alert("meap");
  };
 };
</script>

使用这个

你根本不需要JavaScript。只需将必需的属性用于


这将阻止表单提交,除非选中复选框。请记住,您仍然需要像往常一样进行后端验证,因为如果付出足够的努力,请求可能会伪造。

向我们展示您的尝试。是的,如果可能,您的帮助我将满满当当。Javascript我使用这种语言创建了一个新的Javascript
<script type=”text/javascript”>

 function foo(){
var checkbox = document.getElementById("chbx");
   if(checkbox.checked){
     alert("meap");
  };
 };
</script>
$("#form").submit(function(e) {
    if(!$('input[type=checkbox]:checked').length) {
        alert("Please select the checkbox.");

        //stop the form from submitting
        return false;
    }

    return true;
});

<form id='form'>
    <div>
        <input type="checkbox" name="option-1" id="option-1"> <label for="option-1">Option 1</label>
   </div>
    <div>
        <input type="submit" value="Do thing" >
    </div>
</form>
<form action="index.php?option=com_platiniumchristmas&view=success" method="post">
  <label>
    <input type="checkbox" name="termsConditions" id="chbx" value="Yes" required />
    <?php echo JText::_( 'COM_PLATINIUMCHRISTMAS_TERMSCONDITIONS_TEXT');?>
  </label>
  <input type="submit" value="Emitir Voucher" class="btn btn-success" style="float: right" />
  <div style="overflow: hidden; padding-right: .5em;">
    <input type="text" name="friendName" style="width: 100%;" />
    <input type="hidden" name="canal" value="<?php echo $_POST[" canal "]; ?>"/>
  </div>
</form>