从选项选择单击时打开 2 个 URL

Open 2 URLs on click from option select

本文关键字:URL 选项 选择 单击      更新时间:2023-09-26
在我的

表单中有一个下拉列表,在按钮单击时我需要打开 2 个 URL,如果用户选择选项 1,那么在按钮单击后会出现一个感谢页面,另一个 URL 应该打开。

我目前只能打开一个 URL。请看一下我的小提琴。

http://jsfiddle.net/ty65E/5/

此外,在我的页面中,按钮很少,我希望它们应该首先禁用,这样它们就不会再担心了。 我想强制用户先填写表格,然后只有他可以访问其他按钮。

伙计们感谢您的支持。

您可以尝试的一件事是从选择选项调用onchange函数。我认为它更干净,以后会帮助你。这是我尝试过的

<select name="interest_c" id="interest_c" class="required" onchange="report(this.value)">
  <option selected="selected" value="">Indicate your interest...</option>
  <option value="1">I want a Free Trial</option>
   <option value="2">I want to see a pre-recorded  demo</option>
                                </select>​

<script type="text/javascript">
function report(selectval) {
    switch(parseInt(selectval))  {
        case 1:
            window.open("http://www.stackoverflow.com");
            window.open("http://www.google.com");
            break;   
        case 2:
        window.open("http://www.facebook.com");
        window.open("http://www.php.net");
        break;
    } 
}​
</script>

我在JSFIDDLE上试过 http://jsfiddle.net/eXSND/

希望这有帮助

您可以存储多个 URL,用类似 '|' 的东西分隔它们,然后使用 split 创建 URL 列表。第一个值将始终存在(如果您只有一个 url,split 将生成一个包含单个元素的数组),第二个值仅存在于您指定了两个 URL 的选项中。

<option value="google.com|stackoverflow.com">Two URLs</option>
val = val.split('|');
window.open(val[0]);
if ( val.length > 1 )
    window.open(val[1]);

至于您的另一个问题,请在按钮上添加 disabled 属性,并添加到必须填写的每个输入中:

$(selector_with_all_fields).change(function() {
    var allFilled = true;
    $(selector_with_all_fields).each(function() { 
        allFilled = allFilled && $(this).val() !== "";
    });
    $(my_buttons).attr("disabled",allFilled); // If that doesn't work, try removeAttr when allFilled is false
});

请记住在 HTML 中以"disabled"="true"开头。