如何防止在grails中进行远程提交?

时间:2014-01-09 08:44:34

标签: javascript ajax grails gsp

<g:formRemote name="form1" update="homeBody" 
              url="[controller: 'xxx', action:'aaa']">       
<Button type="submit">Save</Button>    
</g:formRemote>

我的情况是在表单之外有一个文本字段。该值由javascript函数设置。

如果文本字段有值,则表单将提交。如果没有,我必须阻止表单提交。怎么做?

1 个答案:

答案 0 :(得分:1)

grails标记 g:remoteForm 可以具有之前的属性。根据Grails文档,此属性的值为:

  

在远程函数调用之前调用的JavaScript函数。系统会自动添加分号,因此您无需在此字符串中自行提供分号。

因此,您可以通过这种方式阻止表单提交:

<g:formRemote name="form1" update="homeBody" 
              url="[controller: 'xxx', action:'aaa']"
              before="return checkTheField()">
    <Button type="submit">Save</Button>    
</g:formRemote>

checkTheField()是一个javascript函数,返回 true false ,具体取决于字段检查结果。

P.S。我建议您使用grails g:submitButton 标记而不是纯HTML 按钮标记以保持一致性。

相关问题