这是验证表单而不使用“的最佳方式”是什么;花式Javascript“;
我使用序列化数据并将其保存到服务器 我想知道在不使用“花式Javascript”的情况下,在将错误发送到服务器之前,处理错误或验证日期的最佳方法是什么 附言 我只需要检查所有字段是否为空 这是我的密码:这是验证表单而不使用“的最佳方式”是什么;花式Javascript“;,javascript,backbone.js,validation,Javascript,Backbone.js,Validation,我使用序列化数据并将其保存到服务器 我想知道在不使用“花式Javascript”的情况下,在将错误发送到服务器之前,处理错误或验证日期的最佳方法是什么 附言 我只需要检查所有字段是否为空 这是我的密码: var myView = Backbone.View.extend({ submitData: function (event) { event.preventDefault(); var data = backboneSyphon.serialize(
var myView = Backbone.View.extend({
submitData: function (event) {
event.preventDefault();
var data = backboneSyphon.serialize(this);
myModel.save(data, {
success: function () {
},
error: function () {
}
}
}
除非你想做纯粹的服务器端验证,否则你必须使用一些Javascript来做验证;我不太清楚你说的“幻想”是什么意思。既然你在使用主干,那很可能意味着你在使用jQuery(很好,或者ZEPTO),所以我会考虑jQuery处于“Funcess”的界限之内;p> 使用jQuery,我能想到的最简单的JS是:
var failed;
$('#yourForm :input').each(function(input) {
if (!$(input).val()) {
alert("You need to fill in " + input.name);
failed = true;
return false;
}
}
// Do whatever else you want to do if (failed)
下面是一个“非花式javascript”:
window.onload=函数(){
var yourForm=document.getElementById('form'),
yourInput=document.getElementById('name');
yourForm.onsubmit=函数(){
如果(yourInput.value==yourInput.defaultValue | | yourInput.value==''){
警惕(“你必须写点什么”);
返回false;
}
}
}
如果您不想使用花哨的javascript代码,在您的情况下,由于您使用的是主干,
我建议您使用backbone.validation
以下是指向此库的链接:您所说的“花式javascript”是什么意思?请定义“花式javascript”。您是否试图避免使用库,如jQuery或MooTools?OP使用的是主干,这需要jQuery(或者至少是Zepto,这类似于缩小的jQuery);只需重新检查主干站点,就可以在没有jQuery的情况下运行主干(只是没有jQuery,所有视图/DOM操作位都无法工作)。虽然我认为这很少见,但我想OP可能没有使用jQuery或Zepto。
<!DOCTYPE html>
<title></title>
<form id="form" method="post" action="someform.php">
<input id="name" name="Name" value="Name*">
<input type="submit" value="">
</form>
<script>
window.onload = function () {
var yourForm = document.getElementById('form'),
yourInput = document.getElementById('name');
yourForm.onsubmit = function () {
if(yourInput.value == yourInput.defaultValue || yourInput.value == '') {
alert('You must write something');
return false;
}
}
}
</script>