jQuery Click - 在第1页加载时不起作用,但在重新加载页面时有效

时间:2022-12-01 10:19:00

I have an issue with the jQuery click function. I am trying to integrate jQuery Mobile Autocomplete into an C# MVC application.

我有一个jQuery点击功能的问题。我正在尝试将jQuery Mobile Autocomplete集成到C#MVC应用程序中。

It the following code doesn't work when the page first loads. However, it works if I reload/refresh the page.

当页面首次加载时,以下代码不起作用。但是,如果我重新加载/刷新页面,它可以工作。

HTML:

        <ul data-role="listview"  class="selectitems" data-filter="true"  data-inset="true" data-filter-reveal="true" data-filter-placeholder="Search Ingredients...">
            @foreach (var i in Model.ingredientList){
                <li data-id="@i.id" data-unit="@i.useUnit.symbol"><a href="#" class="ui-screen-hidden">@i.title</a></li>
            }
        </ul>

Script:

    <script src="/Scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.mobile-1.3.2.min.js" type="text/javascript"></script>


    $(document).ready(function () {
        $('.selectitems > li').on('click', function () {
            $('input[data-type="search"]').val($(this).text());
            $("ul:jqmData(role='listview')").children().addClass('ui-screen-hidden');
            $('#hdnIngredientID').val($(this).data('id'));
            $('#txtUseQtyDetails').val($(this).data('unit'));
            $('#hdnIngredientTitle').val($(this).text());
            $('#txtQty').focus();
        });

        $('.ui-input-clear').on('tap', function () {
            $('#hdnIngredientID').val('');
            $('#txtUseQtyDetails').val('');
        });
    });

Any assistance would be appreciated.

任何援助将不胜感激。

UPDATE:

jQuery-mobile autocomplete hides the list items until user input happens using CSS "display: none;". Would this prevent the click function from being assigned? If so, how do I work around this?

jQuery-mobile autocomplete隐藏列表项,直到使用CSS“display:none;”进行用户输入。这会阻止分配点击功能吗?如果是这样,我该如何解决这个问题呢?

FURTHER UPDATE:

Found that the "live" function is depreciated. Changed it to "on" instead. Unfortunately this didn't fix the problem :-(

发现“实时”功能已折旧。改为将其改为“on”。不幸的是,这并没有解决问题:-(

Could it be because the "li" items are hidden by CSS when the page is loaded?

可能是因为加载页面时CSS会隐藏“li”项吗?

I've deployed it here:

我在这里部署了它:

http://rar_mobile_dev.runarestaurant.com/Ingredient/Create?recipe_id=15240

(username: test, password: test)

(用户名:test,密码:test)

1 个解决方案

#1


-1  

You could try using on() in stead of click.

您可以尝试使用on()而不是单击。

$(document).on('click', '.selectitems > li', function(){
        $('input[data-type="search"]').val($(this).text());
        $("ul:jqmData(role='listview')").children().addClass('ui-screen-hidden');
        $('#hdnIngredientID').val($(this).data('id'));
        $('#txtUseQtyDetails').val($(this).data('unit'));
        $('#hdnIngredientTitle').val($(this).text());
        $('#txtQty').focus();
});

#1


-1  

You could try using on() in stead of click.

您可以尝试使用on()而不是单击。

$(document).on('click', '.selectitems > li', function(){
        $('input[data-type="search"]').val($(this).text());
        $("ul:jqmData(role='listview')").children().addClass('ui-screen-hidden');
        $('#hdnIngredientID').val($(this).data('id'));
        $('#txtUseQtyDetails').val($(this).data('unit'));
        $('#hdnIngredientTitle').val($(this).text());
        $('#txtQty').focus();
});