最简单的方法是使用以下代码改造标准 HTML 表单。
从一个带有提交按钮的基本工作标准 HTML 表单开始,这将获取您的值并将它们发布到您的表单目的地,在您的表单提交按钮下方返回输出。
现在是仔细检查您是否成功链接到 jquery、语义 javascript 和语义 css 的好时机。
将 class="ui form" 添加到您的表单标签。
在下面添加 javascript。
.
$(document).ready(function() {
// validation
$('.ui.form').form({
email: {
identifier : 'email',
rules: [
{
type : 'email',
prompt : 'Please enter an email'
}
]
}
},
{
inline: true,
on: 'blur',
transition: 'fade down',
onSuccess: validationpassed
});
// called if correct data added to form
function validationpassed() {
// Multiple instances may have been bound to the form, only submit one.
// This is a workaround and not ideal.
// Improvements welcomed.
if (window.lock != "locked") {
var myform = $('.ui.form');
$.ajax({
type: myform.attr('method'),
url: myform.attr('action'),
data: myform.serialize(),
success: function (data) {
//if successful at posting the form via ajax.
myformposted(data);
window.lock = "";
}
});
}
window.lock = "locked";
}
// stop the form from submitting normally
$('.ui.form').submit(function(e){
//e.preventDefault(); usually use this, but below works best here.
return false;
});
function myformposted(data) {
// clear your form and do whatever you want here
$('.ui.form').find("input[type=text], textarea").val("");
//$('.ui.submit.button').after("<div>Message sent. Thank you.</div>");
$('.ui.submit.button').after(data);
}
});
基本形式:
<form action="process.php" method="post" class="ui form">
<div class="field">
<label>title</label>
<input name="email" type="text">
</div>
<input type="submit" class="ui button"/>
</form>
如果您希望错误消息显示在一个框中而不是表单本身中,请将其包含在您的表单中,并删除“inline: true”字样,剩下的就是 Semantic UI:
<div class="ui info message"></div>
注意:在语义 UI 中使用表单标签并不是绝对必要的,因为您只需要一个带有“ui form”类的 div,但是这个改造代码确实需要一个表单标签。