簡體   English   中英

僅選擇Html.dropdownlist之外的最近輸入

[英]Selecting only the closest input outside of a Html.dropdownlistfor

我有三個dropdownlistfor集,其中包含三個單獨的輸入,這些輸入在從列表中選擇值“ other”時顯示。 以下代碼僅用於選擇與單擊的項目最接近的輸入。

    Set 1
              @Html.DropDownListFor(m => m.Title, "--Select Title --", new { @class = "form-control requiredField dropListFunction", required="true" })

    set2
                @Html.TextBoxFor(m => m.TitleOther, new { @class = "form-control hidden", placeholder = "Enter New Title" }) 

              @Html.DropDownListFor(m => m.Branch "--Select Branch --", new { @class = "form-control requiredField dropListFunction", required = "true" })

         @Html.TextBoxFor(m => m.BranchOther, new { @class = "form-control hidden", placeholder = "Enter New Branch" })  

Set 3   
        @Html.DropDownListFor(m => m.State, Enum.GetNames(typeof(State)).Select(e => new SelectListItem { Text = e }), "--Select State --", new { @class = "form-control requiredField dropListFunction", required = "true" })

 @Html.TextBoxFor(m => m.StateOther, new { @class = "form-control hidden", placeholder = "Enter New State" })

以下jquery處理最接近dropListFunction的輸入的選擇,其中包含具有其他值的clicked選項

   $('option[value=Other]').on('click',function(){
            var nextInput = $('.dropListFunction').next('input');
            nextInput.removeClass('hidden')
        });

問題在於它不只是選擇列表中的下一個項目,而是在選擇時打開所有隱藏的輸入。 任何幫助是極大的贊賞

您所有的DropDown都具有.dropListFunction類,因此您將獲得所有后續輸入。

$('.dropListFunction').on('change', function(e){
    var dd = $(e.target), input = dd.next('input');
    if(dd.val() === 'other'){
      input.removeClass('hidden');
    }else{
      input.addClass('hidden');
    }
  });

在這里看到jsbin

https://jsbin.com/lacimoyula/edit?html,控制台,輸出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM