HTML: target=“_blank” for a drop-down list

前端 未结 4 1850
谎友^
谎友^ 2021-01-27 13:53

Problem: to open a new window with the select -option

相关标签:
4条回答
  • 2021-01-27 14:28

    You can open your links by window.open() :

    <select name="allSelect" id="allSelect">
    <optgroup label="Historical">
    <option value="http://www.something.com/cse?cx=0000000000000&sa=Search&q=">Open in a new window 1</option>
    <option value="http://www.google.com/cse?cx=0000000000000000A-cmjrngmyku&ie=UTF-8&sa=Search&q=">Open in a new window 2</option>
    </optgroup>
    </select>
    
    <input type="button" 
        value="open in a new window" 
        onclick="window.open(document.getElementById(allSelect).value);" />
    
    0 讨论(0)
  • 2021-01-27 14:36

    Modify your handleSubmit function as follows:

    function handleSubmit()
    {
        var form = _gel("moduleForm"),
            elm = _gel("allQuery"),
            selectElm = _gel("allSelect");
        if (elm != "" && selectElm != "") {
            var query = elm.value;
            var searchUrl = selectElm.value;
            if (query != "" && searchUrl != "") {
                searchUrl += escape(query);
                window.open(searchUrl, form.target || "_blank");
            }
        }
        return false;
    }
    
    0 讨论(0)
  • 2021-01-27 14:39

    Give a look to the window.open function.

    0 讨论(0)
  • 2021-01-27 14:43

    Keep in mind that your page should be usable without scripting, so I'd suggest implementing a fallback mechanism: The form should call a server-side script which responds with a 30x status and a Location header.

    The client-side would look like this:

    <form action="path-to-redirection-script" method="GET" target="_blank"
     onsubmit="window.open(this.elements['foo'].value); return false;">
     <select name="foo" size="1">
      <option value="http://google.com">google</option>
     </select>
     <input type="submit" value="go">
    </form>
    

    Also, remember that target="_blank" / window.open() is often evil.

    0 讨论(0)
提交回复
热议问题