【问题标题】:Dynamic change label and placeholder depends on loaded html动态更改标签和占位符取决于加载的 html
【发布时间】:2017-12-21 20:45:38
【问题描述】:

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

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

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');
}

【问题讨论】:

    标签: javascript php jquery html laravel


    【解决方案1】:

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

    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');
    }
    

    【讨论】:

      猜你喜欢
      • 2022-10-14
      • 2013-03-30
      • 2020-11-28
      • 2017-02-28
      • 2019-05-22
      • 1970-01-01
      • 2014-04-02
      • 2022-11-06
      • 2017-11-02
      相关资源
      最近更新 更多