Html 如何将 <option><select> 中的值传递给表单操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14149088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:43:37  来源:igfitidea点击:

How to pass value from <option><select> to form action

htmlformssubmitactionform-submit

提问by mr.data1

I need to pass a value in option select to action where I have agent_id=

我需要将选项 select 中的值传递给我有 agent_id= 的操作

Can anyone help?

任何人都可以帮忙吗?

<form method="POST" action="index.php?action=contact_agent&agent_id=">
<select>
<option value="1">Agent Homer</option>
<option value="2">Agent Lenny</option>
<option value="3">Agent Carl</option>
</select>

回答by Shoaib

You don't have to use jQuery or Javascript.

您不必使用 jQuery 或 Javascript。

Use the nametag of the select and let the form do it's job.

使用name选择的标签,让表单完成它的工作。

<select name="agent_id" id="agent_id">

回答by mkungla

Like @Shoaib answered, you dont need any jQuery or Javascript. You can to this simply with pure html!

就像@Shoaib 回答的那样,您不需要任何 jQuery 或 Javascript。您可以使用纯 html 简单地做到这一点!

<form method="POST" action="index.php?action=contact_agent">
  <select name="agent_id" required>
    <option value="1">Agent Homer</option>
    <option value="2">Agent Lenny</option>
    <option value="3">Agent Carl</option>
  </select>
  <input type="submit" value="Submit">
</form>
  1. Remove&agent_id=from form action since you don't need it there.
  2. Addname="agent_id"to the select
  3. Optionallyadd word requireddo indicate that this selection is required.
  1. &agent_id=从表单操作中删除,因为您在那里不需要它。
  2. 添加name="agent_id"到选择
  3. 可选地添加单词requireddo 表示需要此选择。

Since you are using PHP, then by posting the form to index.phpyou can catch agent_idwith $_POST

由于您使用PHP,然后张贴到窗体index.php你能赶上agent_id$_POST

/** Since you reference action on `form action` then value of $_GET['action'] will be contact_agent */
$action = $_GET['action'];

/** Value of $_POST['agent_id'] will be selected option value */
$agent_id = $_POST['agent_id']; 

As conclusion for such a simple task you should not use any javascript or jQuery. To @FelipeAlvarez that answers your comment

作为这样一个简单任务的结论,您不应该使用任何 javascript 或 jQuery。回复您评论的@FelipeAlvarez

回答by Birhan Nega

you can simply use your own code but add name for the select tag

您可以简单地使用自己的代码,但为选择标签添加名称

<form method="POST" action="index.php?action=contact_agent&agent_id=">
    <select name="agent_id">
        <option value="1">Agent Homer</option>
        <option value="2">Agent Lenny</option>
        <option value="3">Agent Carl</option>
    </select>

then you can access it like this

然后你可以像这样访问它

String agent=request.getparameter("agent_id");

回答by M Rostami

with jQuery :
html :

使用 jQuery :
html :

<form method="POST" name="myform" action="index.php?action=contact_agent&agent_id="  onsubmit="SetData()">
  <select name="agent" id="agent">
    <option value="1">Agent Homer</option>
    <option value="2">Agent Lenny</option>
    <option value="3">Agent Carl</option>
  </select>
</form>

jQuery :

jQuery :

$('form').submit(function(){
   $(this).attr('action',$(this).attr('action')+$('#agent').val());
   $(this).submit();
});

javascript :

javascript :

function SetData(){
   var select = document.getElementById('agent');
   var agent_id = select.options[select.selectedIndex].value;
   document.myform.action = "index.php?action=contact_agent&agent_id="+agent_id ; # or .getAttribute('action')
   myform.submit();
}

回答by Anthony Gray

instead of trying to catch both POST and GET responses - you can have everything you want in the POST.

而不是试图同时捕捉 POST 和 GET 响应 - 您可以在 POST 中拥有您想要的一切。

Your code:

您的代码:

<form method="POST" action="index.php?action=contact_agent&agent_id=">
  <select>
    <option value="1">Agent Homer</option>
    <option value="2">Agent Lenny</option>
    <option value="3">Agent Carl</option>
  </select>
</form>

can easily become:

很容易变成:

<form method="POST" action="index.php">
  <input type="hidden" name="action" value="contact_agent">
  <select name="agent_id">
    <option value="1">Agent Homer</option>
    <option value="2">Agent Lenny</option>
    <option value="3">Agent Carl</option>
  </select>
  <button type="submit">Submit POST Data</button>
</form>

then in index.php - these values will be populated

然后在 index.php - 这些值将被填充

$_POST['action'] // "contact_agent"
$_POST['agent_id'] // 1, 2 or 3 based on selection in form...