dpyoh6553 2017-01-04 17:29
浏览 399
已采纳

带有下拉菜单的html表单在提交按钮上提交表单

I am trying to create a form. I want my customer to be able to input text into a text field, and then choose a item from a drop down, the drop down is built from a database query which turns this into json and then a selected value of the json is presented in the dropdown. I have attached a image which i hope explains it better. form drawing

  • 写回答

1条回答 默认 最新

  • dszpyf4859 2017-01-04 18:12
    关注

    I'm not sure that's what you are waiting for, but since you are not answering...

    If you want to prevent this : event.preventDefault();

    event.preventDefault(); is there just as a band-aid fix. EG It prevents a default behavior. However in your code there is nothing that says the form should be submitted when you click on the button so it should not be submitted. My guess is that you are not using a button, but a submit button instead.

    In any case you can use the onsubmit to make check prior to submitting:

    onsubmit="validateMyForm()"
    

    and prevent the default behavior if some conditions are not met.


    You said that those links are here as a place holder. This confused me a lot before reading this so I have to edit my answer once more. Please next time use something a bit more irrelevant like spans, or div.

    Yes, you can have input in this drop down, to answer your comment.


    Secundo, do you have multiple dropdowns ?

    window.onclick = function(event) {
      if (!event.target.matches('.dropbtn')) {
    
        var dropdowns = document.getElementsByClassName("dropdown-content");
        var i;
        for (i = 0; i < dropdowns.length; i++) {
          var openDropdown = dropdowns[i];
          if (openDropdown.classList.contains('show')) {
            openDropdown.classList.remove('show');
          }
        }
      }
    }
    

    This part will loop through all elements with the dropdown class and you have only 1 element so why the loop ? Maybe you have multiple dropdowns, so clicking anywhere else on the page will close all your dropdowns.

    if (!event.target.matches('.dropbtn'))

    What does that matches ? The button. So clicking anywhere except on the button will make the dropdowns close. But do you want to close the drop down when you click on an item of said dropdown?


    What I'm trying to convey to you with this is that there seems to be flaws in the user experience you are trying to make.

    further reading:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误