JQuery 验证插件:防止嵌套表单上的验证问题
我有一个使用验证的表单。该表格有一个用于照片上传的部分。最初,此部分包含六个元素,代码如下:
<img class="photo_upload" src="image/app/photo_upload.jpg">
我已将一个函数绑定到 photo_upload
类的单击事件。此函数使用以下代码将图像替换为最小形式: 复制代码
<form onsubmit="startUploadImage();" target="control_target"
enctype="multipart/form-data" method="post" action="index.php">
<input type="hidden" value="add_image" name="service">
<input type="hidden" value="1000000" name="MAX_FILE_SIZE">
<input type="file" size="10" name="photo" id="photo_file_upload"><br>
<button onclick="javascript:cancel_photo_upload();return false;">Cancel</button>
</form>
因此,本质上,单击图像后,我将在原始的经过验证的表单中嵌套一个新表单。现在,当我使用这个新表单并上传图像时,我收到一条错误(重复三次):
validator is undefined
http://host.com/path/index.php
Line 286
这是怎么回事?我的猜测是这个
- 提交事件冒泡到外部表单
- 当我们对该表单进行验证时,验证被触发,
- 验证尝试找到触发它的表单,
- 因为我们没有将验证绑定到内部表单,所以它
现在返回“未定义”,我的评估正确吗?不管是与否,我该如何解决这个问题?
I have a form on which I use validation. This form has a section for photo upload. Initially this section contains six elements with the following code:
<img class="photo_upload" src="image/app/photo_upload.jpg">
I have bound a function to the click event for the class of photo_upload
. This function replaces the image with a minimal form with this code:
Copy code
<form onsubmit="startUploadImage();" target="control_target"
enctype="multipart/form-data" method="post" action="index.php">
<input type="hidden" value="add_image" name="service">
<input type="hidden" value="1000000" name="MAX_FILE_SIZE">
<input type="file" size="10" name="photo" id="photo_file_upload"><br>
<button onclick="javascript:cancel_photo_upload();return false;">Cancel</button>
</form>
So, essentially, after the image is clicked, I'd have a new form nested in my original, validated form. Now, when I use this new form and upload an image, I receive an error (repeated three times) saying:
validator is undefined
http://host.com/path/index.php
Line 286
What is going on here? My guess is this
- Submit event bubbles up to the outer form
- As we have validation on that form, validation is triggered,
- Validation tries to find the form triggering it,
- Since we have not bound validation to the inner form it returns 'undefined'
Now, is my assessment correct? Whether it is or not, how can I solve this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HTML 中不允许嵌套表单。您需要使用带有两个提交按钮的单个表单,并设置服务器端表单处理程序,以便它识别哪个按钮是
使用并相应地分支。
Nested forms are not allowed in HTML. You need to use a single form with two submit buttons and to set up the server-side form handler so that it recognizes which button was
used and branches accordingly.
如果您不想更改页面结构,可以通过修补事件处理函数
delegate
来修复此错误。jquery.validate.js v1.15.0,第 401 - 408 行:
我的修补版本:
修补缩小版本(jquery.validate.js 被打包以使用 grunt 和 uglify 进行分发)更困难,但并非不可能。
function delegate(event)
变为function b(b)
。另一种方法是修补绑定
delegate
时使用的选择器。我认为这是一个更复杂的修复,但浏览器性能会稍微好一些。If you don't want to alter your page structure, you can fix this error by patching the event handling function
delegate
.jquery.validate.js v1.15.0, lines 401 - 408:
My patched version:
Patching the minified version (jquery.validate.js is packaged for distribution using grunt with uglify) is harder but not impossible.
function delegate(event)
becomesfunction b(b)
.An alternative approach would be to patch the selector used when binding
delegate
. I consider that a more complex fix, but browser performance would be marginally better.