从“图像”类型的输入提交数据通过 Ajax 而不是表单提交
基本上,我想使用图像输入,但我不想提交表单来获取结果,我想从单击图像输入中获取X和Y坐标,然后通过ajax提交它们。
现在,我意识到我可以将图像输入放入其自己的表单中,然后提交到隐藏的 iframe 并按时间间隔检查结果(我现在正在为 ajax 上传器执行此操作) ,不过好像有点做作。 我还意识到我可以通过 JavaScript 从任何点击中获取鼠标位置,这并不是我真正要问的
我可以使用这样的图像类型输入吗?有没有办法让点击图像输入设置它的值,然后调用一些 JavaScript 来执行我的 ajax 调用?
Basically, I want to use an image input, but I don't want to have to submit form to get the result, I want to get the X and Y coordinates from the click on the image input, then submit them via ajax.
Now, I realize I can just put the image input in its own form, then submit to a hidden iframe and check for a result on an interval (I'm doing that for an ajax uploader right now), but it seems a little hokey. I also realize I can get my mouse position via javascript from any click, that's not really what I'm asking
Can I use the input of type image like this? Is there a way to have the click on the image input just set it's value then call some JavaScript to execute my ajax call?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我正在检查我的旧问题,我发现这个问题从未得到充分的回答。我不记得我是否“很久以前”就想到了这一点,但我现在知道该怎么做。
只需检查图像输入的点击事件的 offsetX 和 offsetY 即可。它应该偏移到您单击的图像输入。
I was going through my old questions and I saw that this one was never answered adequately. I don't remember if I figured this out "way back when", but I do know how to do it now.
Just check the offsetX and offsetY of the click event of an image input. It should be offset to the image input you clicked on.
看看这里:
http://jsfiddle.net/pfLtm/
如您所见,该代码可以轻松修改为如下所示:
这会将点击的 X 和 Y 位置存储为元素本身的属性。
测试用例: http://jsfiddle.net/pfLtm/1/
您可以调用而不是返回 false例如 AJAX 调用。
Take a look here:
http://jsfiddle.net/pfLtm/
As you see, that code can be easily modified to something like this:
This will store the X and Y position of the click as attributes of the element itself.
Test case: http://jsfiddle.net/pfLtm/1/
Instead of returning false you can invoke the AJAX call for example.