gpt4 book ai didi

javascript - 如何在不使用
元素的情况下触发 html5 验证?

转载 作者:行者123 更新时间:2023-11-27 22:58:21 26 4
gpt4 key购买 nike

我有一个正常的表单,看起来像这样(你可以忽略内部字段,因为它们都是输入字段):

<div class="col-md-12" style="float: none;">
<div class="form-group row">
<div class="col-md-6" style="border: 2px solid #efefef;">
<div class="card-body">
<div class="col-md-12">
<h4>Type</h4>
</div>
</div>
</div>
<div class="col-md-6" style="border: 2px solid #efefef;">
<div class="card-body">
<div class="form-group row ">
<div class="col-md-4">
<label>Appeal Reason</label>
</div>
<div class="col-md-3">
<label>Appeal Amount</label>
</div>
<div class="col-md-3">Penalty</div>
<div class="col-md-2"></div>
<div class="col-md-4">
<input type="text"
class="form-control"
id="applReason"
name="applReason" required> <span
id="fromDateError" style="color: red; font-weight: bold"></span>
</div>
<div class="col-md-3">
<input type="number"
class="form-control"
id="applAmount" name="applAmount"
required>
<span id="toDateError" style="color: red; font-weight: bold"></span>
</div>
<div class="col-md-3">
<input type="number" class="form-control" id="applPenalty"
name="applPenalty" required> <span
id="consignmentNoError"
style="color: red; font-weight: bold"></span>
</div>
<div class="col-md-2">
<button type="submit">+</button>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-12">
<h5>Remarks</h5>
<textarea rows="4" cols="100">

</textarea>
</div>
<button type="submit" id="sub" class="btn btn-success pull-right">Submit</button>
</div>

由于某些原因,我没有将所有这些字段都包装在<form></form> 中标记,但是当我通过单击 <button type="submit" id="sub" class="btn btn-success pull-right">Submit</button> ,它不执行 html5 验证。此点击功能已触发并已发布。如何在不使用 <form> 的情况下验证 html5 功能标签?

$( "#sub" ).click(function() {
alert( "Handler for .click() called." );
//ajax code to submit
});

最佳答案

您可以为此使用约束验证 API。引用this .示例-

var emailId = document.getElementById("id");
var valid = emailId . checkValidity();
if (valid) {
//perform operation
}

关于javascript - 如何在不使用 <form> 元素的情况下触发 html5 验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54210683/

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