【问题标题】:Add text to input field instead of replacing it with ListView + jQuery Mobile向输入字段添加文本,而不是用 ListView + jQuery Mobile 替换它
【发布时间】:2013-02-03 18:30:13
【问题描述】:

我想要完成的事情:

单击一个按钮会添加一个词来过滤 jQuery Mobile ListView。

HTML:

<ul data-role="listview" data-theme="a" data-divider-theme="c">
                <li data-role="list-divider" data-inset="true">
                    <div data-role="controlgroup"  data-type="horizontal" data-mini="true" id="filterButton">
                        <a href="#" data-role="button" data-inline="true">Iedereen</a>
                        <a href="#" data-role="button" data-inline="true">LinkedIn</a>
                        <a href="#" data-role="button" data-inline="true">Facebook</a>
                        <a data-role="button" style="width: 4px;"></a>
                        <a href="#" data-role="button" data-inline="true" class="filterAdd">Design</a>
                        <a href="#" data-role="button" data-inline="true">Development</a>
                    </div>
                </li>
            </ul>
            <ul data-role="listview" data-filter="true" data-theme="d" data-divider-theme="d" data-filter-placeholder="Search Connections...">
                <li data-theme="b"><a href="index.html">
                    <img src="http://a0.twimg.com/profile_images/1353509401/DSC_3465_bigger.jpg" class="ui-li-thumb zapp-corner zapp-margin-5 zapp-inner-shadow" />
                    <h3>Stephen Weber</h3>
                    <p><strong>You've been invited to a meeting at Filament Group in Boston, MA</strong></p>
                    <p>Hey Stephen, if you're available at 10am tomorrow, we've got a meeting with the jQuery team.</p>
                    <p class="ui-li-aside"><strong>LinkedIn</strong></p>
                </a></li>

JavaScript

$(document).ready(function () {
                    $("#filterButton").delegate("a", "click", function () {
                        $("div.ui-input-search").find("input").val($(this).text());
                        $("div.ui-input-search").find("input").keyup();
                    });
}); 

此时它会替换输入字段中的整个值,而不是将其添加到现有值中。

http://jsfiddle.net/J4HZp/6/

【问题讨论】:

    标签: jquery listview mobile filter


    【解决方案1】:
    $("#filterButton").delegate("a", "click", function () {
         var txt = $(this).text();
         $("div.ui-input-search").find("input").val(function(i, currentValue) {
            return currentValue + txt;
         }).trigger('keyup');
    });
    

    【讨论】:

    • 这正是他要求的,但我会.trigger('keyup')。我真的不认为在这种情况下这是一个太大的问题,但大多数时候人们只是想触发附加到事件处理程序的任何函数,而不是触发它本身的事件。
    • 这成功了!我使用 .trigger('keyup') 因为否则我两次得到相同的结果(它重复了这个词)。 jsfiddle.net/J4HZp/8
    猜你喜欢
    • 2016-03-21
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    相关资源
    最近更新 更多