微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – .formValidation不是一个函数

原始问题:

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

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: {

有了这个输出

Screen shot of js console

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

解决方法:

由于您正在使用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>

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

Result

我希望它对你有所帮助.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐