【问题标题】:change form fields based on dropdown values根据下拉值更改表单字段
【发布时间】:2016-09-20 07:50:19
【问题描述】:

您好,我有一个 html,一开始我有下拉列表,如何根据下拉值的选择更改表单字段? 这是代码。就像我选择一般查询一样,它应该显示前 2 个表单域和一般 div 中的表单域

<form action="">
    <select name="cases" id="cases">
        <option value="general">General Inquiry</option>
        <option value="credit">Credit Inquiry</option>
        <option value="payment">Payment Issue</option>
    </select><br>
    <label for="email">Email Address <span>*</span></label>
    <input type="text">
    <label for="full name">Full Name <span>*</span></label>
    <input type="text">


    <div class="general" id="general">
        <label for="documents">Wish to submit any requested documents?</label>
        <input type="radio" name="radio">Yes
        <input type="radio" name="radio">No <br><br>
        <label for="select">How did you find out about us?<span>*</span></label><br>
        <select name="case" id="case-type">
            <option value="value1">Value 1</option>
            <option value="value2">Value 2</option>
            <option value="value3">Value 3</option>
        </select><br>
    </div>

    <div class="credit" id="credit">
        <label for="Date of Inquiry">Date of Inquiry<span>*</span></label>
        <input type="date">
        <label for="Agency">Agency 3 <span>*</span></label>
        <input type="text">         
    </div>

    <div class="payment" id="payment">
        <label for="Service Phone Number">Service Phone Number<span>*</span></label>
        <input type="text">
        <label for="select">Topic<span>*</span></label><br>
        <select name="case" id="case-type">
            <option value="topic1">Topic 1</option>
            <option value="topic2">Topic 2</option>
            <option value="topic3">Topic 3</option>
        </select><br><br>
    </div>
    <br><br>
    <button>Submit</button>
</form>

如果我选择 Credit Inquiry 它应该显示前 2 个表单域和 credit div 中的表单域 如果我选择付款查询,它应该显示前 2 个表单字段和付款 div 中的表单字段

【问题讨论】:

  • 您需要编写 javascript 函数来决定隐藏哪些文件和显示哪些文件。更改下拉菜单
  • 是的,我需要决定该功能的 Javascript。

标签: javascript jquery html forms drop-down-menu


【解决方案1】:

在这里聚会迟到了,但这是另一种方式

    // hide all the divs
    $('div').hide()

    // Show and hide selected div
    $('#cases').change(function () {
        var value = this.value;

        $('div').hide()
        $('#' + this.value).show();
    });

还创建了demo

【讨论】:

    【解决方案2】:

    如果这是你的意思,只需隐藏所有 div 并在选择更改时显示相应的 div

    $('div').hide()
    $('#cases').change(function(){
        var v=this.value;
        $('div').hide().filter(function(){return this.id==v}).show()
    });
    

    【讨论】:

      【解决方案3】:

      我写的是“一般”案例..你可以在其他案例中遵循这个

        $("#cases").change(function () {
                  var case = $( "#cases option:selected" ).val();
                  if(case=="general")
                  {
                    //show 2 form fields here and show div
                     $("#general").show();
                  }
              });
      

      【讨论】:

      • @Muthu,这并不是一个真正的好方法,原因是如果更多项目被添加到列表中,您将需要更新更改方法
      猜你喜欢
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多