Remote: true形式避免js字段验证
remote: true form avoids js field validation
这是我的表单:
<%= form_for [@company,@company_branch], :html => { :onsubmit => "return validateName();" }, remote: true do |f| %>
<%= f.text_field :name %>
...
当我不使用远程调用时,"validateName"验证工作正常。但是如果远程操作,表单仍然会被提交,即使我的验证返回false。
这是用于验证的.js:
function validateName() {
var name = document.getElementById('name');
if( name.value == "" ) {
highlightError(name, "Please provide a name");
return false;
}
return ( true );
}
我也撞了。我目前还没有找到一个解释,但根据我的实验,远程做表单的post独立于onsubmit函数的返回值。我在服务器端使用表单验证,并发回一个js来处理验证错误。
更新。"在Rails 2中?至少3,form_form,:remote => true覆盖:onsubmit => ' func(); '方法来执行实际的表单提交。如果您想在表单提交之前(或提交期间或提交之后)绑定某些内容,请使用jQuery.bind()绑定表单,然后观察AJAX回调函数来执行所需的操作。
<script type="text/javascript>
function get_address_information() {
// check jquery for all the possible callbacks. there is also success and error. compete get calls after both success and error
var pars = 'param1=x&param2=y&param3=z';
$.ajax({ type: "POST",
url: "/get_invite_address",
data: pars, dataType: 'script',
beforeSend: function() {
// do your before send checking here
},
complete: function(data, status) {
// do some post processing ehre
}
});
}
</script>"
形成源
变体工作解决方案是将验证绑定到ajax: beforeend事件,函数beforeOneClick返回false或true:
$('form')
.bind('ajax:success', function(evt, data, status, xhr) {
console.log('success: ' + xhr + '/' + status + '/' + data);
})
.bind("ajax:beforeSend", function(evt, xhr, settings){
return beforeOneClick();
})
.bind('ajax:complete', function(evt, xhr, status){
console.log('complete');
})
.bind("ajax:error", function(evt, xhr, status, error){
console.log('error:' + xhr + '/' + status + '/' + error);
});
相关文章:
- 带有let的JS/EECMAScript6私有字段的模式
- JS动态添加字段-删除按钮不起作用
- 我怎么能让流星简单模式中的一个字段等于一个js变量,并且仍然让用户填写简单模式的其余部分
- 处理字段中带有换行符的csv文件-node.js
- 无法格式化keystone.js中的嵌套日期字段
- 表单使用js、html,而不是隐藏字段
- MaskMoney.js无法处理动态添加字段
- 在Drupal7中扩展字段集时,修改collapse.js以从xml获取附加数据
- Ember js 2计数相关字段(hasMany)
- 如何将typeahead.js(Bloodhound)添加到jQuery动态创建的字段中
- 角度 JS 动态选项卡字段
- Javascript 单击html字段值而不编辑.js文件
- 在 ember.js 中自定义输入字段的最佳实践
- Angular JS字段仅当其值大于tan零时有效
- JS 字段验证 - 循环遍历对象,每个字段有三个数据注意事项
- Remote: true形式避免js字段验证
- 致命错误:CALL_AND_RETRY_2分配失败-进程在预处理我的js字段时内存不足
- 如何成功地设置一个值在JS字段?试穿了.专注但没有运气
- JS字段渲染返回标准字段渲染
- 抓取Parsley JS字段错误和更新CSS