Javascript - 在新窗口中选择元素打开URL并提交表单 [英] Javascript - form select element open url in new window and submit form

查看:91
本文介绍了Javascript - 在新窗口中选择元素打开URL并提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新 - 请在原始问题下方阅读更多详细信息

UPDATED - please read further details below original question

我有一个包含各种网址的精选表单元素,我想在其中打开选择一个新窗口 - 要做到这一点,我在元素的onchange事件中有以下代码:

I have a select form element with various urls, that I want to open in a new window when selected - to do this I have the following code in the element's onchange event:

window.open(this.options[this.selectedIndex].value,'_blank');

这很好用。但是我也想在更改这个选择元素值时提交表单 - 我尝试了各种各样的东西,但我似乎无法让它工作。

This works fine. But I also want to submit the form when changing this select element value - I've tried various things, but I can't seem to get it to work.

I有jquery,所以如果通过它更容易实现,那就没关系。

I have jquery, so if it's easier to achieve via that then that's fine.

更新 - 我刚刚意识到上面还有另一个问题,因为有些url实际上用于生成和输出pdf,这些不起作用 - 它们打开然后立即关闭(至少在IE7中)。

更新07/05/09 - 我现在已经开启了这个问题的赏金,因为我真的需要提出一个有效的解决方案。我最初通过显示链接而不是表单选择元素来解决问题,但这不再可行。

UPDATE 07/05/09 - I've now opened a bounty for this question as I really need to come up with a working solution. I did originally get around the issue by displaying links instead of a form select element, but this is no longer feasible.

我需要上述原因的原因是我有一个可能需要查看/打印的大量文件,太多而无法合理地显示为链接列表。我需要提交表单以记录查看/打印特定文件的事实,然后在表单上显示文件历史记录的日志 - 我很擅长实现这方面的事情,所以不需要那里的帮助,但我认为放置背景会有所帮助。

The reason I need the above is that I have a large number of files that might need to be viewed / printed, too many to reasonably display as a list of links. I need to submit the form to record the fact a particular file has been viewed / printed, then display a log of the file history on the form - I'm comfortable with achieving this side of things, so don't require assistance there, but I thought it would help to place the context.

因此,为了澄清我的要求 - 我需要一个表单选择元素和查看按钮,单击它时不仅会在新窗口中启动文件下载(请注意以上问题我在这些文件是PDF时遇到的问题,但也提交包含select元素的表单。

So, to clarify my requirements - I need a form select element and 'View' button that when clicked will not only launch a file download in a new window (note the above issue I faced when these files were PDFs), but also submit the form that contains the select element.

推荐答案

这里你去了

<script type="text/javascript">
    $(function() {
        $("#selectElement").change(function() {
            if ($(this).val()) {
                window.open($(this).val(), '_blank');
                $("#formElement").submit();
            }
        });

        // just to be sure that it is submitting, remove this code
        $("#formElement").submit(function() {
            alert('submitting ... ');
        });
    });
</script>

<form id="formElement" method="get" action="#">
    <select id="selectElement">
        <option></option>
        <option value="http://www.deviantnation.com/">View Site 1</option>
        <option value="http://stackoverflow.com/">View Site 2</option>
        <option value="http://serverfault.com/">View Site 3</option>
    </select>
</form>

这篇关于Javascript - 在新窗口中选择元素打开URL并提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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