简体   繁体   中英

Hide div based on selected dropdown

This code works fine when you are now creating a new page. Selecting a dropdown will show or hide as decorated by the markup. The problem is in an edit page with a default selected Id like Id 3 I want the div decorated with 3 to be hidden on page load. I am completely at sea with javascript and jquery.

<div class="form-group">
<label class="control-label col-md-2" for="ArticleCategoryId">Menu Category</label>
    <div class="col-md-10">
    <select class="chooseOption form-control" id="ArticleCategoryId" name="ArticleCategoryId">
    <option value="1">pages</option>
    <option value="2">about</option>
    <option selected="selected" value="3">project</option>
    <option value="4">gallery</option>
    <option value="5">news</option>
    <option value="6">events</option>
    <option value="7">FAQS</option>
    <option value="8">Jobs</option>
    <option value="9">Documents</option>
    <option value="10">Clients</option>
    </select>

 <div class="form-group ArticleCategoryId 3">
 <label class="control-label col-md-2" for="ArticleOnDate">Start Date</label>
     <div class="col-md-10">
    <input class="form-control text-box single-line" data-val="true" data-val-date="The field Start Date must be a date." id="ArticleOnDate" name="ArticleOnDate" type="datetime" value="" />
    <span class="field-validation-valid text-danger" data-valmsg-for="ArticleOnDate" data-valmsg-replace="true"></span>
                </div>
            </div>

            <div class="form-group ArticleCategoryId 1">
                <label class="control-label col-md-2" for="ArtilceOnTime">On Time</label>
                <div class="col-md-10">
                    <input class="form-control text-box single-line" id="ArtilceOnTime" name="ArtilceOnTime" type="text" value="" />
                    <span class="field-validation-valid text-danger" data-valmsg-for="ArtilceOnTime" data-valmsg-replace="true"></span>
                </div>
            </div>

Below is the jquery snippet

<script type="text/javascript">
jQuery(".optionName").hide();
jQuery("document").ready(function () {   /// have to wait till after the document loads to run these things
jQuery("select.chooseOption").change(function () {
jQuery("." + this.id).hide();
var thisValue = jQuery(this).val();
if (thisValue != "")
 jQuery("." + thisValue).show();
        });
    });
</script>

This being an edit page I want "<div class="form-group ArticleCategoryId 3">....</div> to be hidden on page load since item Id 3 has been selected but the other should show. Any help will be appreciated

Having components classes given such as "ArticleCategoryId 1" is a bad idea.

You could have sth like

<div class="form-group editable-category" data-category-id="1">
   <label class="control-label col-md-2" for="ArtilceOnTime">On Time</label>
   <div class="col-md-10">
      <input class="form-control text-box single-line" id="ArtilceOnTime" name="ArtilceOnTime" type="text" value="" />
      <span class="field-validation-valid text-danger" data-valmsg-for="ArtilceOnTime" data-valmsg-replace="true"></span>
   </div>
</div>

and css

.editable-category{
  display:none;
}

.editable-category.active{
  display:block;
}

and js as follow

$(document).ready(function(){
  $('select.chooseOption').on('change',function(){
     var thisValue = $(this).find('option:selected').val();
     if(thisValue){
       $('.editable-category.active').removeClass('active');
       $('.editable-category[data-category-id="'+thisValue+'"]').addClass('active');
     }
  }).trigger('change');

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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