jQuery 提交在 Opera 中不起作用

发布于 2024-11-02 16:28:31 字数 776 浏览 4 评论 0原文

我有一个简单的表单,可以通过一个小的 jQuery 脚本在元素外部提交。

<script type="text/javascript">
    $(document).ready(function() {
        $('#mybutton').click(function(){
            $('#myform').attr('action', '/url/to/form').submit();
        });
    });
</script>

按钮只是一个普通的链接

<a href="javascript:void(0);" id="mybutton">Just a normal link</a>

表单只是一个普通的表单

<form id="myform" action="/url/to/form">
    ....
    <input type="submit" value="Search">
</form>

这在 IE、FF、Chrome 和 Safari 中工作正常,但不适用于 Opera。在 Opera 中,它总是重定向到我的主页,而不是表单操作的 url。我尝试在脚本中设置操作(如上所述),并且通常在表单操作参数中设置操作,但没有成功。我正在运行最新的 jQuery 和 Opera。

请帮助

编辑:我还使用 jQuery UI 框架来处理一些内容和交互

I have a simple form which can be submitted outside the element via a small jQuery Script.

<script type="text/javascript">
    $(document).ready(function() {
        $('#mybutton').click(function(){
            $('#myform').attr('action', '/url/to/form').submit();
        });
    });
</script>

The button is just a normal link

<a href="javascript:void(0);" id="mybutton">Just a normal link</a>

The form is just a normal form

<form id="myform" action="/url/to/form">
    ....
    <input type="submit" value="Search">
</form>

This works fine in IE, FF, Chrome, and Safari but not for Opera. In Opera it always redirects to my home page, not to the url of the form action. I have tried setting the action in the script (as above) and normally within the form action parameter but with no luck. I'm running that latest jQuery and Opera.

Please help

Edit: I'm also using the jQuery UI framework to handle some of the content and interactions

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光清浅 2024-11-09 16:28:31

我要做的第一件事是更改处理程序以防止链接的默认操作:

    $('#mybutton').click(function(ev){
        ev.preventDefault();
        $('#myform').attr('action', '/url/to/form').submit();
    });

对按钮使用 标记意味着您必须小心浏览器不会对语义。我没有太多在 Opera 上调试东西的经验,但这是我首先怀疑的事情。

如果没有真正的“href”与“button”关联,我会质疑为什么它是一个 标签;它可能只是一个简单的 ,它也可以附加一个点击处理程序,但不具有任何潜在问题的本机行为。

First thing I would do is change the handler to prevent the default action for the link:

    $('#mybutton').click(function(ev){
        ev.preventDefault();
        $('#myform').attr('action', '/url/to/form').submit();
    });

Using <a> tags for buttons means that you have to be careful that the browser doesn't get confused over the semantics. I don't have a lot of experience debugging things on Opera, but that's the first thing I'd suspect.

If there's no real "href" associated with the "button", I would question why it's an <a> tag at all; it could be just a simple <span>, which can also have a click handler attached but which does not have any potentially-problematic native behavior.

最丧也最甜 2024-11-09 16:28:31

我相信这与当前不完整的 jquery ui select 类有关。它添加了一个带有 href 的 a 标签,如 # 我相信这可能是您的问题。

I believe it will be something to do with, with the currently incomplete jquery ui select class. It adds an a tag with a href as # i believe this may be your problem.

一个人练习一个人 2024-11-09 16:28:31

尝试使用 $('#myform').submit(); 而不是 $('#myform').attr('action', '/url/to/form ').submit();

编辑:正如另一个答案所说,在对点击事件执行任何操作之前,您首先必须调用 event.preventDefault();因为这会阻止浏览器重定向 你。

Edit2:正确的代码是:

<script type="text/javascript">
$(document).ready(function() {
    $('#mybutton').click(function(event){
        event.preventDefault();
        $('#myform').attr('action', '/url/to/form').submit();
    });
});

这应该可以正常工作。

Try just $('#myform').submit(); instead of $('#myform').attr('action', '/url/to/form').submit();

Edit: As the other answer said, you will first have to call event.preventDefault(); before doing anything on the click event, as that prevents the browser from redirecting you.

Edit2: The right code would be:

<script type="text/javascript">
$(document).ready(function() {
    $('#mybutton').click(function(event){
        event.preventDefault();
        $('#myform').attr('action', '/url/to/form').submit();
    });
});

This should work fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文