如何给选定的组合框动作

How to give action to selected combo box

本文关键字:组合      更新时间:2023-09-26

嗨,我想使用按id和按日期搜索

<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和提交,所以它会去控制器('web/execute_search'),然后如果我选择日期和提交,去控制器('web/execute_search')。

试试下面的代码:

<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>
使用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;来获取文本,表示您想要排序的方式。

希望对你有帮助!!