将页面重定向到不同的URL,使用选择下拉菜单和提交按钮。

huangapple go评论61阅读模式
英文:

Redirect page to different URL using select dropdown and submit button

问题

我想创建一个表单,当用户从下拉菜单中选择一个选项并点击提交按钮时,将用户重定向到不同的URL。以下是我的表单的标记:

<form>
  <select>
    <option selected="">- 选择 -</option>
    <option value="https://google.com">One</option>
    <option value="https://gmail.com">Two</option>
    <option value="https://yahoo.com">Three</option>
  </select>
  <input type="submit" value="提交">
</form>

Is there anything else you'd like to know or need assistance with?

英文:

I want to create a form that redirect the user to a different URL when they select an option from the select dropdown and click the submit button. Here is the markup for my form below:

&lt;form&gt;
  &lt;select&gt;
    &lt;option selected=&quot;&quot;&gt;- Select -&lt;/option&gt;
    &lt;option value=&quot;https://google.com&quot;&gt;One&lt;/option&gt;
    &lt;option value=&quot;https://gmail.com&quot;&gt;Two&lt;/option&gt;
    &lt;option value=&quot;https://yahoo.com&quot;&gt;Three&lt;/option&gt;
  &lt;/select&gt;
  &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;

答案1

得分: 0

Sure, here is the translated code:

<select id="select_location">
  <option selected="">- 选择 -</option>
  <option value="https://google.com">One</option>
  <option value="https://gmail.com">Two</option>
  <option value="https://yahoo.com">Three</option>
</select>
<input type="button" value="提交" onclick="window.location=document.getElementById('select_location').value;">
英文:
  &lt;select id=&quot;select_location&quot;&gt;
    &lt;option selected=&quot;&quot;&gt;- Select -&lt;/option&gt;
    &lt;option value=&quot;https://google.com&quot;&gt;One&lt;/option&gt;
    &lt;option value=&quot;https://gmail.com&quot;&gt;Two&lt;/option&gt;
    &lt;option value=&quot;https://yahoo.com&quot;&gt;Three&lt;/option&gt;
  &lt;/select&gt;
  &lt;input type=&quot;button&quot; value=&quot;Submit&quot; onclick=&quot;window.location=document.getElementById(&#39;select_location&#39;).value;&quot;&gt;

答案2

得分: 0

你可以这样做:

<form action="yourLink">

但是,如果你像Oriol Vilaseca建议的那样使用onclick,你可能需要阻止提交事件:

event.PreventDefault

英文:

You can do:

&lt;form action=&quot;yourLink&quot;&gt;

but if you use onclick as @Oriol Vilaseca suggested, you may need to prevent the submit event:
event.PreventDefault

答案3

得分: 0

以下是翻译好的部分:

SCRIPT部分:

  <script>
    let selectList = null;
    let listButton = null;

    window.addEventListener("DOMContentLoaded", getElements);

    function getElements(e) {
      selectList = document.getElementById("select_location");
      listButton = document.getElementById("submitButton");
      listButton.addEventListener("click", clickHandler);
    }

    function clickHandler(e) {
      if (selectList.value != "select") {
        var win = window.open(selectList.value, "_top");
      } else {
        alert("在三个选项中进行选择!");
      }
    }
  </script>

HTML部分:

  <select id="select_location">
    <option selected="" disabled value="select">- 选择 -</option>
    <option value="https://google.com">One</option>
    <option value="https://gmail.com">Two</option>
    <option value="https://yahoo.com">Three</option>
  </select>

  <input type="button" value="提交" id="submitButton">
英文:

This solution will work but not in the SO snippet:

SCRIPT PART :

  &lt;script&gt;
	let selectList=null;
	let listButton=null;
	
	window.addEventListener(&quot;DOMContentLoaded&quot;, getElements);
	
	function getElements(e){
		selectList = document.getElementById(&quot;select_location&quot;);
		listButton = document.getElementById(&quot;submitButton&quot;);
		listButton.addEventListener(&quot;click&quot;, clickHandler);
	}
	
	function clickHandler(e){
		if(selectList.value!=&quot;select&quot;){
			var win = window.open(selectList.value,&quot;_top&quot;);
		}else{
			alert(&quot;Chose between the three options!&quot;);
		}
	}
  &lt;/script&gt;

HTML PART :


	  &lt;select id=&quot;select_location&quot;&gt;
		&lt;option selected=&quot;&quot; disabled value=&quot;select&quot;&gt;- Select -&lt;/option&gt;
		&lt;option value=&quot;https://google.com&quot;&gt;One&lt;/option&gt;
		&lt;option value=&quot;https://gmail.com&quot;&gt;Two&lt;/option&gt;
		&lt;option value=&quot;https://yahoo.com&quot;&gt;Three&lt;/option&gt;
	  &lt;/select&gt;

	&lt;input type=&quot;button&quot; value=&quot;Submit&quot; id=&quot;submitButton&quot;&gt;

答案4

得分: -1

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->
    <select>
        <option selected="">- 选择 -</option>
        <option value="https://google.com">One</option>
        <option value="https://gmail.com">Two</option>
        <option value="https://yahoo.com">Three</option>
    </select>
    <button onclick="window.location=document.querySelector('select').value;">提交</button>

<!-- end snippet -->
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;select&gt;
    &lt;option selected=&quot;&quot;&gt;- Select -&lt;/option&gt;
    &lt;option value=&quot;https://google.com&quot;&gt;One&lt;/option&gt;
    &lt;option value=&quot;https://gmail.com&quot;&gt;Two&lt;/option&gt;
    &lt;option value=&quot;https://yahoo.com&quot;&gt;Three&lt;/option&gt;
&lt;/select&gt;
&lt;button onclick=&quot;window.location=document.querySelector(&#39;select&#39;).value;&quot;&gt;Submit&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月20日 23:29:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定