2017-07-17 22 views
2

我在后端使用jquery和laravel。我有两个php刀片加载的搜索栏。我想占位符和标签动态变化取决于加载的PHP刀片文件。这就是它现在的样子,它只显示第一个标签和占位符,甚至在刀片中不存在id也显示第一个标签和占位符。动态更改标签和占位符取决于加载的html

HTML/BLADE.PHP

<div class="container"> 
    <div class="row"> 
     <div class="show-hide-section"> 
      <button class="btn btn-success show-hide-search-bar">Pokaż wyszukiwarkę</button> 
     </div> 
     <div class="col-xs-12 col-md-12"> 
      <div class="searcher-section" style="display: none"> 
       <div class="show-hide-searcher"> 
        <div class="input-section"> 

         <div class="label-input-searcher"> 
          <label for="" class="searcher-label" id="label-searchbar"></label> 

          <input type="text" placeholder="" class="searcher-input form-control" id="input-searchbar" /> 
          <div class="null-data" style="display: none;">Wprowadź poprawne dane</div> 
         </div> 
        </div> 
       </div> 
      </div> 
     </div> 
    </div> 
</div> 

JS jQuery对象的

if ($('#agents') !== null) { 
    $('#label-searchbar').html('Imię, Nazwisko, Adres email'); 
    $('#input-searchbar').attr('placeholder','Podaj imię, nazwisko lub adres email'); 
} else if ($('#company') !== null) { 
    $('#label-searchbar').html('Nazwa biura'); 
    $('#input-searchbar').attr('placeholder','Podaj nazwę biura'); 
} 

回答

1

使用.length属性来检查的元件的存在。 $(selector)将返回一个jQuery对象,它永远不会是null

if ($('#agents').length > 0) { 
    $('#label-searchbar').html('Imię, Nazwisko, Adres email'); 
    $('#input-searchbar').attr('placeholder','Podaj imię, nazwisko lub adres email'); 
} else if ($('#company').length > 0) { 
    $('#label-searchbar').html('Nazwa biura'); 
    $('#input-searchbar').attr('placeholder','Podaj nazwę biura'); 
}