繁体   English   中英

.formValidation不是一个函数

[英].formValidation is not a function

原始问题:

我的表单正确地验证了验证,但是在不应该的时候提交。

关注http://formvalidation.io/examples/ajax-submit/

查看我的控制台我没有看到.on “错误”部分中的任何日志语句或打印出的“成功”。 页面只是发布到自己。 随着网址中的数据。 我不知道我在这里缺少什么,因为它基本上会跳过所有.on语句。

这里发生了什么?

请记住,验证仅适用于.on语句。

HTML:

<form id="service_path_form" class="form-horizontal">
<div class="form-group">
    <label class="col-xs-3 control-label">Service Name</label>
    <div class="col-xs-8">
        <input type="text" class="form-control" name="service_path_name" placeholder="Name" />
    </div>
</div>

<div class="form-group">
    <label class="col-xs-3 control-label">IP</label>
    <div class="col-xs-5">
        <input type="text" class="form-control" name="service_path_ip" placeholder="IP" />
    </div>
</div>

<div class="form-group">
    <label class="col-xs-3 control-label">Description</label>

    <div class="col-xs-8">
        <textarea class="form-control" name="service_path_description" rows="3" placeholder="Write a short Description"></textarea>
    </div>
</div>

<div class="form-group">
    <label class="col-xs-3 control-label">Enable</label>
    <div class="col-xs-5">
        <div class="radio">
            <label>
                <input type="radio" name="service_path_enabled" value="1" /> Enabled
            </label>
        </div>
        <div class="radio">
            <label>
                <input type="radio" name="service_path_enabled" value="0" /> Disabled
            </label>
        </div>
    </div>
</div>

<div class="form-group">
    <div class="col-xs-9 col-xs-offset-4">
        <button type="submit" class="btn btn-primary" name="validate" value="Validate and Submit">Validate and Submit</button>
    </div>
</div>
<input type="hidden" name="created_by" value="{{request.user}}">
<input type="hidden" name="date_created" id = "date_created" value="">

JS:

<script>
$(document).ready(function() {
    $('#service_path_form')
      .bootstrapValidator({
        framework: 'bootstrap',
        icon: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            service_path_name: {
                // The messages for this field are shown as usual
                validators: {
                    notEmpty: {
                        message: 'The Service Path Name is required'
                    },
                }
            },
            service_path_ip: {
                // Show the message in a tooltip
                err: 'tooltip',
                validators: {
                    notEmpty: {
                        message: 'The destination ip address is required'
                    },
                    ip: {
                        message: 'The ip address is not valid'
                    }
                }
            },
            service_path_enabled: {
                // Show the message in a tooltip
                err: 'tooltip',
                validators: {
                    notEmpty: {
                        message: 'Do you want this service path to be actively monitored?'
                    }
                }
            }
        }
    })
    .on('err.form.fv', function(e) {
      e.preventDefault();
      console.log(e)
      console.log('test')
    })
    .on('success.form.fv', function(e) {
      // Prevent form submission
      console.log("MADE IT HERE")
      e.preventDefault();

      var $form = $(e.target),
          fv    = $form.data('formValidation');

      // Use Ajax to submit form data
      console.log("MADE IT HERE")
      $.ajax({
          url: "/servicepathapi/v1/servicepaths/",
          type: 'POST',
          data: $form.serialize(),
          success: function(result) {
              console.log(result)
          }
      });
  })
});
</script>

当前:

建议升级更新:

我试过@Arkni的建议,虽然我觉得它正朝着正确的方向发展,我现在得到了这个:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="{% static 'app/scripts/js/bootstrap/bootstrap.min.js' %}" ></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script src="{% static 'form_validation/js/formValidation.js' %}" ></script>
<script src="{% static 'form_validation/js/framework/bootstrap.min.js' %}" ></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>


<script>
$(document).ready(function() {
    $('#service_path_form')
      .formValidation({
        framework: 'bootstrap',
        icon: {

有了这个输出:

js控制台的屏幕截图

FIX:在检查我的所有源文件时,我注意到我实际上正在加载jquery 2.0.2和2.1.3,删除2.0.2修复了问题。

正如您在问题中所说,您使用的是FormValidation,而不是BootstrapValidator

所以要解决你的问题,请更换它

$(document).ready(function() {
    $('#service_path_form')
        .bootstrapValidator({ // <====

$(document).ready(function() {
    $('#service_path_form')
        .formValidation({ // <====

一些说明:

由于您正在使用FormValidation,因此我应该尝试删除jquery.validate.min.js,因为我害怕冲突。

这个jsfiddle正常工作:

<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/vendor/formvalidation/css/formValidation.min.css">

<form id="service_path_form" class="form-horizontal">
<div class="form-group">
    <label class="col-xs-3 control-label">Service Name</label>
    <div class="col-xs-8">
        <input type="text" class="form-control" name="service_path_name" placeholder="Name" />
    </div>
</div>

<div class="form-group">
    <label class="col-xs-3 control-label">IP</label>
    <div class="col-xs-5">
        <input type="text" class="form-control" name="service_path_ip" placeholder="IP" />
    </div>
</div>

<div class="form-group">
    <label class="col-xs-3 control-label">Description</label>

    <div class="col-xs-8">
        <textarea class="form-control" name="service_path_description" rows="3" placeholder="Write a short Description"></textarea>
    </div>
</div>

<div class="form-group">
    <label class="col-xs-3 control-label">Enable</label>
    <div class="col-xs-5">
        <div class="radio">
            <label>
                <input type="radio" name="service_path_enabled" value="1" /> Enabled
            </label>
        </div>
        <div class="radio">
            <label>
                <input type="radio" name="service_path_enabled" value="0" /> Disabled
            </label>
        </div>
    </div>
</div>

<div class="form-group">
    <div class="col-xs-9 col-xs-offset-4">
        <button type="submit" class="btn btn-primary" name="validate" value="Validate and Submit">Validate and Submit</button>
    </div>
</div>
</form>

脚本:

<script src="//code.jquery.com/jquery-2.1.3.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="/vendor/formvalidation/js/formValidation.min.js"></script>
<script src="/vendor/formvalidation/js/framework/bootstrap.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
$('#service_path_form')
  .formValidation({
    framework: 'bootstrap',
    icon: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        service_path_name: {
            // The messages for this field are shown as usual
            validators: {
                notEmpty: {
                    message: 'The Service Path Name is required'
                },
            }
        },
        service_path_ip: {
            // Show the message in a tooltip
            err: 'tooltip',
            validators: {
                notEmpty: {
                    message: 'The destination ip address is required'
                },
                ip: {
                    message: 'The ip address is not valid'
                }
            }
        },
        service_path_enabled: {
            // Show the message in a tooltip
            err: 'tooltip',
            validators: {
                notEmpty: {
                    message: 'Do you want this service path to be actively monitored?'
                }
            }
        }
    }
  });
});
</script>

以下结果显示了它的样子:

结果

我希望它对你有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM