繁体   English   中英

如何在jQuery的select2下拉搜索框中仅允许数字?

[英]How to allow only numbers in select2 dropdown search box in jquery?

我在jQuery中使用select2下拉列表,我只允许数字而不允许空格和字符在搜索框中。

我的代码是:

<script>
   $(document).ready(function () {

        //The url we will send our get request to
        var attendeeUrl = '@Url.Action("GetAttendees", "Home")';
        var pageSize = 20;

        $('#attendee').select2(
        {
            placeholder: 'Enter name',
            //Does the user have to enter any data before sending the ajax request
            minimumInputLength: 0,            
            allowClear: true,
            multiselect:true,
            ajax: {
                //How long the user has to pause their typing before sending the next request
                quietMillis: 150,
                //The url of the json service
                url: attendeeUrl,
                dataType: 'jsonp',
                //Our search term and what page we are on
                data: function (term, page) {
                    debugger;
                    return {
                        pageSize: pageSize,
                        pageNum: page,
                        searchTerm: term
                    };
                },


                results: function (data, page) {
                    //Used to determine whether or not there are more results available,
                    //and if requests for more data should be sent in the infinite scrolling                    
                    var more = (page * pageSize) < data.Total; 
                    return { results: data.Results, more: more };
                }
            }
        });

    });

</script>

尝试类似的东西

$(document).ready(function () {
  //called when key is pressed in textbox
  $("#quantity").keypress(function (e) {
     //if the letter is not digit then display error and don't type anything
     if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
        //display error message
        $("#errmsg").html("Digits Only").show().fadeOut("slow");
               return false;
    }
   });
});

demo1的

DEMO2

暂无
暂无

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

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