如何对选定的组合框进行操作

时间:2016-09-10 01:21:11

标签: javascript html5

Hi, i want to use search by id and search by date

<select id="menu">
    <option value="<?=site_url('web/execute_search')?>">By Id</a></option>
    <option value="<?=site_url('web/execute_searchs')?>">By Date</a></option>
    </select>
    <input type="text" name="search" />
    <input type="submit" value="Submit" />

我有组合框,当我选择按ID搜索并提交以便它将转到Controller(&#39; web / execute_search&#39;),然后如果我选择日期并提交,请转到Controller(&#39; ;网页/ execute_search&#39;。)

2 个答案:

答案 0 :(得分:0)

试试这段代码:

<select id="menu" onchange="option()">
    <option value="1" content="1">By Id</a></option>
    <option value="2" content="2">By Date</a></option>
    </select>

    <input type="text" name="search" />
    <input type="submit" value="Submit" />

然后是javascript:

<script>function option(){
var x = document.getElementById("menu").value;
if (x==1){
    window.location.href = "execute_search";
}
if (x==2){
window.location.href = "execute_searchs";
}}</script>

答案 1 :(得分:0)

  

使用Javascript

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function findID()
{

     var e = document.getElementById("menu");
     var clicked = e.options[e.selectedIndex].value;
     var text = e.options[e.selectedIndex].text;
      if(clicked ==1)
      {
      window.location.href = "web/execute_search";
      }
      if (clicked ==2)
      {
      window.location.href= "web/execute_searchs";
      }
     alert(clicked); 
     alert(text);

}
</script>
</head>
<body>

<select id="menu" name="menu">
    <option value="1">By Id</option>
    <option value="2">By Date</option>
</select>
    <input type="text" name="search" />
    <input type="submit" value="Submit" onclick="findID();" />

</body>
</html>

使用e.options[e.selectedIndex].value;获取值。 使用e.options[e.selectedIndex].text;来获取文本,意味着您想要排序的方式。

希望它能帮到你!!