Firefox 处理 xxx.submit(),Safari 不处理……可以做什么? [英] Firefox handles xxx.submit(), Safari doesn't ... what can be done?

查看:18
本文介绍了Firefox 处理 xxx.submit(),Safari 不处理……可以做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户选择(释放鼠标)菜单中的一个选项时,我试图让下拉菜单发布一个表单.此代码在 FF 中工作正常,但 Safari 出于某种原因不提交表单.我使用 jquery 重新编写了代码,以查看 jquery 的 .submit() 实现是否更好地处理了浏览器的怪癖.同样的结果,在 FF 中工作在 safari 中不起作用.

I'm trying to make a pull down menu post a form when the user selects (releases the mouse) on one of the options from the menu. This code works fine in FF but Safari, for some reason, doesn't submit the form. I re-wrote the code using jquery to see if jquery's .submit() implementation handled the browser quirks better. Same result, works in FF doesn't work in safari.

以下代码片段来自同一页面,其中混合了一些 django 模板语言.

The following snippets are from the same page, which has some django template language mixed in.

这是香草 js 尝试:

Here's the vanilla js attempt:

function formSubmit(lang) {
    if (lang != '{{ LANGUAGE_CODE }}') {
        document.getElementById("setlang_form").submit();
    }
}

这是 jquery 尝试:

Here's the jquery attempt:

$(document).ready(function() {
    $('#lang_submit').hide()
    $('#setlang_form option').mouseup(function () { 
        if ($(this).attr('value') != '{{ LANGUAGE_CODE }}') { 
            $('#setlang_form').submit()
        } 
    });
});

这是表格:

<form id="setlang_form" method="post" action="{% url django.views.i18n.set_language %}">
    <fieldset>
    <select name="language">
    {% for lang in interface_languages %}
        <option value="{{ lang.code }}" onmouseup="formSubmit('{{ lang.name }}')" {% ifequal lang.code LANGUAGE_CODE %}selected="selected"{% endifequal %}>{{ lang.name }}</option>
    {% endfor %}
    </select>
    </fieldset>
</form>

我的问题是,如何在 Safari 中使用它?

My question is, how can I get this working in Safari?

推荐答案

代码如下:

<form id="setlang_form" method="post" action="{% url django.views.i18n.set_language %}">
    <fieldset>
    <select name="language" onchange="formSubmit(this)">
    {% for lang in interface_languages %}
        <option value="{{ lang.code }}" {% ifequal lang.code LANGUAGE_CODE %}selected="selected"{% endifequal %}>{{ lang.name }}</option>
    {% endfor %}
    </select>
    </fieldset>
</form>

获取值:

function formSubmit(theForm)
{
 .... theForm.options[theForm.selectedIndex].value
}

你也可以用 jquery 做到:

You can do it with jquery too:

$(document).ready(function() {
    $('#lang_submit').hide()
    $('#setlang_form select').change(function () { 
        ....     $("select option:selected").text() ....  
        } 
    });
});

看这里了解有关 Jquery 的更改事件:http://docs.jquery.com/Events/change

Look here to know about change event with Jquery: http://docs.jquery.com/Events/change

这篇关于Firefox 处理 xxx.submit(),Safari 不处理……可以做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆