调用事件处理程序

时间:2016-06-25 11:39:26

标签: javascript jquery

在下面的代码中,您可以看到数据从下拉菜单文本中获取值我需要数据从href获取值并将#替换为""获取没有#sgin

的值
<script type="text/javascript">
    $(function () {
        SearchText();
    });
    function SearchText() {
        $(".autosuggest").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Default2.aspx/GetAutoCompleteData",
                    data: "{'value':'" + $('#<%= txtSearch.ClientID %>').val() + "',$('.search-panel .dropdown-menu').find('a').click();}", dataType: "json",
                    success: function (data) {
                        if (data.d.length > 0) {
                            response($.map(data.d, function (item) {
                                return {
                                    label: item.split('/')[0],
                                    val: item.split('/')[1]
                                }
                            }));
                        }
                        else {
                            response([{ label: 'No Records Found', val: -1 }]);
                        }
                    },
                    error: function (result) {
                        alert("Error");
                    }
                });
            },
        });
    }
</script>

标记

<div class="input-group">
    <div class="input-group-btn search-panel">
        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
            <span id="search_concept">Filter by</span> <span class="caret"></span>
        </button>

        <ul class="dropdown-menu" role="menu">
            <li><a href="#UserID">User ID</a></li>
            <li><a href="#TypeName">User Type</a></li>
        </ul>


    </div>

提前致谢

2 个答案:

答案 0 :(得分:1)

它是一个将在点击时调用的事件处理程序,但您可以这样调用它:

$('.search-panel .dropdown-menu').find('a').click();

答案 1 :(得分:0)

可能这很有用。让我们把所有的功能代码放在文档之外,并根据触发的事件加载正文时调用函数。

<script type="text/javascript">
    $(document).ready(function (e) {
//calling the function
     function myEvents();
    });
   //now you can define  the function as per your requirement
   function myEvents(){
     $('.search-panel .dropdown-menu').find('a').click(function (e) {
            e.preventDefault();
            var param = $(this).attr("href").replace("#", "");
            var concept = $(this).text();
            $('.search-panel span#search_concept').text(concept);
            return concept;
        });   
    };

</script>