简体   繁体   English

防止选择框更改

[英]Prevent select box from changing

I'm working on an angular js 2 project and I need to create a dropdown (kinda like a link) wherein if you select an option it will trigger an onchange event and redirect the user. 我正在开发一个角度js 2项目,我需要创建一个下拉列表(有点像链接),如果您选择一个选项,它将触发onchange事件并重定向用户。 Currently I have this: 目前我有这个:

<select id = "dashboard" onchange="goToPage()" class="form-control">
  <option value="" selected disabled>{{ 'Create' | translate }}</option>
  <option value="Applicant" >{{ "dashboard.applicant" | translate}}</option>
  <option value="Reports">{{ "dashboard.report" | translate}}</option>
</select>

In the select box I want the 'Create' option to always be selected even if the applicant selects Applicant or Reports. 在选择框中,即使申请人选择“申请人”或“报告”,我也希望始终选择“创建”选项。 How can I do this behavior? 我该怎么做?

You can't cast onchange if user can't change selected value at all, but you can place inside your function this line document.getElementById("dashboard").selectedIndex = 0; 如果用户根本无法更改所选值,则无法onchange ,但是您可以在函数内部放置以下行: document.getElementById("dashboard").selectedIndex = 0; which will select first option again: 它将再次选择第一个选项:

 function goToPage(){ document.getElementById("dashboard").selectedIndex = 0; } 
 <select id = "dashboard" onchange="goToPage()" class="form-control"> <option value="" selected disabled>{{ 'Create' | translate }}</option> <option value="Applicant" >{{ "dashboard.applicant" | translate}}</option> <option value="Reports" >{{ "dashboard.report" | translate}}</option> </select> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM