gpt4 book ai didi

javascript - Abide Foundation - 触发 valid.fndtn.abide

转载 作者:行者123 更新时间:2023-11-29 23:48:22 33 4
gpt4 key购买 nike

我正在使用 Abide 在表单上运行验证,效果很好。但是,如果表单有效或无效,我无法执行操作。

HTML:

<form role="form" id="form_search_extended" name="form_search_extended" data-abide="ajax" novalidate>

<input name="postalCode" type="text" value="{{$data['postalCode']['value']}}" placeholder="ZIP/Postal Code" required maxlength="10">

<button type="submit" value="Submit">Submit</button>

</form>

Javascript:

    $('#form_search_extended')
.on('invalid.fndtn.abide', function () {
callFunctionOnValid();
console.log('invalid!');
})
.on('valid.fndtn.abide', function () {
console.log('valid!');
});

var form = document.getElementById('form_search_extended');
form.onsubmit = function(e) {

e.preventDefault();

console.log('form submitted');
};

有效或无效监听器中的代码未运行。

我不明白为什么这不起作用。 valid.fndtn.abide 的事件监听器是否未被触发? Abide 正在运行,因为显示了对字段的验证。

谢谢。

最佳答案

您需要初始化脚本。

添加

  <script>
$(document).foundation();
</script>

在结束正文标签之前

$('#form_search_extended')
.on('valid.fndtn.abide', function(e, el) {
callFunctionOnValid();
console.log('invalid!');
})
.on('invalid.fndtn.abide', function(e, el) {
console.log(e.target, 'valid!');
})
.on("submit", function(ev) {
ev.preventDefault();
console.log("Submitted");
});
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/css/normalize.min.css" rel="stylesheet" type="text/css" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/css/foundation.min.css" rel="stylesheet" type="text/css" />

</head>

<body>

<form data-abide novalidate role="form" id="form_search_extended" name="form_search_extended">
<label>Number required
<input name="postalCode" type="text" placeholder="ZIP/Postal Code" required pattern="number" maxlength="10">
</label>
<small class="error">You need a number.</small>
<button type="submit" value="Submit">Submit</button>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/js/vendor/modernizr.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/js/vendor/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/js/foundation.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/js/plugins/foundation.abide.min.js"></script>
<script>
$(document).foundation();
</script>
</body>

</html>

关于javascript - Abide Foundation - 触发 valid.fndtn.abide,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43395985/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com