当组合框包含特定单词时更改操作

时间:2014-04-25 16:46:57

标签: javascript html combobox

我将尝试解释这个问题比上一次更好,就像上次我做得非常非常糟糕一样!

到目前为止,我有代码,它会显示一个组合框和一个按钮。在我的组合框中,有3个选项,名为" StaffHome"," AdminHome"和#34; IntranetHome"。

这是我需要做的: 如果" StaffHome"选中并按下按钮,页面重定向到' /staffhome/index.html'

如果" AdminHome"选中并按下按钮,页面重定向到' /adminhome/hyd/home.html'

如果" IntranetHome"选中并按下按钮,页面重定向到' /intrhme/index.html'。

我想用JS来做这件事。

<form id="select">
    <select>
        <option value="staffhome">StaffHome</option>
        <option value="adminhome">AdminHome</option>
        <option value="intranethome">IntranetHome</option>
    </select>
    <button type="button">Redirect</button>
</form>

fiddle

1 个答案:

答案 0 :(得分:0)

为按钮添加ID

对于Plain JS,选择一个名称或ID

var actions =['/staffhome/index.html','/adminhome/hyd/home.html', '/intrhme/index.html']; 
window.onload=function() {
  document.getElementById("but").onclick=function() {
    this.form.action=actions[this.form.folderSelect.selectedIndex]; // using name
    this.form.submit();
  }
}

或者将按钮设为提交按钮并执行

window.onload=function() {
  document.getElementById("select").onsubmit=function() {
    this.action=actions[document.getElementById("folderSelect").selectedIndex]; // using ID
  }
}

Live Demo