简体   繁体   English

Semantic-UI Popup - 在初始化之前更改HTML

[英]Semantic-UI Popup - Change HTML before initializing

I'm working with Semantic-UI and have a popup with an input field in it (search field). 我正在使用Semantic-UI,并在其中有一个带有输入字段的弹出窗口(搜索字段)。 The popup is initialized/opened "on: 'click'", but I would like to set a value to the input field with javascript before the user opens the popup. 弹出窗口初始化/打开“on:'click'”,但我想在用户打开弹出窗口之前用javascript为输入字段设置一个值。

Trying to do so with 试着这样做

document.getElementById("sok_tekst").value = input;

only leads to 只会导致

Uncaught TypeError: Cannot set property 'value' of null 未捕获的TypeError:无法设置null的属性“value”

When I look in the console, I see that the HTML-code contains the popup with the input field (id="sok_tekst"), but it looks like I "can't reach it" with javascript before it's initialized. 当我查看控制台时,我发现HTML代码包含带有输入字段的弹出窗口(id =“sok_tekst”),但看起来我在初始化之前使用javascript“无法访问它”。

I feel I have tried everything, and it looks like I need to open the popup before adding a value to the input field. 我觉得我已经尝试了所有东西,看起来我需要在输入字段中添加值之前打开弹出窗口。

Here is a DEMO 这是一个DEMO

Do anyone have a trick up their sleeve for this?? 有没有人有这个伎俩?

Thanks! 谢谢!

For the curious: I am using the input field as a search field in a table/work plan. 对于好奇:我使用输入字段作为表/工作计划中的搜索字段。 When the table is redrawn, by the user changing week number, the popup are redrawn as well. 重绘表时,通过用户更改周数,弹出窗口也会重新绘制。 I'm trying to keep the search value, and put it back in the input field, in the new popup, in the new table. 我正在尝试保留搜索值,并将其放回输入字段,新弹出窗口,新表中。

In order to initialize input value you shouldn't specify popup content with data-html="..." , this won't let change the value cause the input is not yet created (this is why you're getting Cannot set property 'value' of null) , so should change it from a pre-existing HTML content , so use instead popup: $('#YourContentSelector'), in popup setting to set the content ,then and you'll be able to change input value using : $('#input_test').val('test'); 为了初始化输入值,你不应该用data-html="..."指定弹出内容,这不会让值更改因为还没有创建输入(这就是为什么你得到的不能设置属性'value'为null) ,所以应该从预先存在的HTML内容中改变它,所以请使用popup: $('#YourContentSelector'),在弹出设置中设置内容,然后你就可以改变输入值使用: $('#input_test').val('test'); like the following : 如下:

HTML: HTML:

<!-- Popup Button -->
<i class="search link icon sok"></i>
<!-- Popup Button -->

<!-- Popup Content -->
<div class='ui form popup hidden' id="content"><!-- hidden class added so it won't be shown when it's loaded   -->
    <div class='field'>
        <div class='ui icon input'>
            <input type='text' placeholder='Input' id='input_test'>
            <i class='search icon'></i>
        </div>
    </div>
</div>
<!-- Popup Content -->

JS(jQuery) JS(jQuery的)

$(document).on('click', '.search.sok', function() {
  $('#input_test').val('test');
    $(this)
       .popup({
           popup: $('#content'),
            on:'click'
       })
       .popup('show');

});

Here is a working Demo 这是一个有效的演示

Docs 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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