简体   繁体   English

NAV-LINK - 在重定向之前测试用户输入?

[英]NAV-LINK - test for user input before redirect?

I want to make sure that the user has made a selection from a dropdown before they are able to redirect to another page via a NAV-LINK in an <li> .我想确保用户在能够通过<li>中的 NAV-LINK 重定向到另一个页面之前从下拉列表中进行了选择。 Is it possible to test for the users selection first and prevent the redirection by keeping them on the page?是否可以先测试用户选择并通过将它们保留在页面上来防止重定向?

I'm able to fire the onclick event but I'm failing to stop the re-direction.我能够触发onclick事件,但我无法停止重定向。 Can any please help me?任何人都可以帮助我吗?

<script>
function testInput(){ 
    alert("please make a selection..");
}
</script>

. .

  <li class="nav-item" onclick="testInput()">
      <a class="nav-link" href="adduser.php">
          <span data-feather="Add User"></span>
          Add User
    </a>
</li>

You can do that by checking whether the dropdown has been changed or not;您可以通过检查下拉菜单是否已更改来做到这一点;

document.getElementById('id_of_your_dropdown').addEventListener('change', () => {
     place your code here which redirects to the other page
})

Here is an example which prints some text to the console when some option is selected from the dropdown;这是一个示例,当从下拉列表中选择某个选项时,它会向控制台打印一些文本;

 document.getElementById('dd').addEventListener('change', () => { console.log('some value selected'); })
 <select id='dd'> <option>first</option> <option>second</option> <option>third</option> <option>forth</option> </select>

Do you want this?你想要这个吗?

<li class="nav-item">
    <a class="nav-link" href="adduser.php" onclick="testInput(event)">
      <span data-feather="Add User"></span>
      Add User
    </a>
  </li>

<select id = "drop-down">
    <option value ="none" disabled selected>Select an option</option>
    <option value = "1">1</option>
    <option value = "2">2</option>
    <option value = "3">3</option>
</select>

<script>
function testInput(e){
    if (document.getElementById("drop-down").value == "none") {
        e.preventDefault();
        alert("please make a selection..");
    }}
</script>

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

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