Javascript 使用空格键触发ajax

Javascript 使用空格键触发ajax,javascript,jquery,ajax,Javascript,Jquery,Ajax,我的函数valueS()由于某种原因无法工作,它不会触发底部的ajax函数。。。由于某些原因,添加空格时不会触发bind(“更改键控输入”) 如何修复函数值()以触发底部函数 $(document).keypress(function(event) { switch(event.which){ case 32: if(!$('input').is(':focus')){ event.preventDefault();

我的函数valueS()由于某种原因无法工作,它不会触发底部的ajax函数。。。由于某些原因,添加空格时不会触发bind(“更改键控输入”)

如何修复函数值()以触发底部函数

$(document).keypress(function(event) {
    switch(event.which){
        case 32:
            if(!$('input').is(':focus')){
               event.preventDefault();
               valueS();
            }
            break;
        //other cases not shown here
    }


function valueS(){
    var value = parseInt(document.getElementById('id1').value, 10);
    value = isNaN(value) ? "" : value;
    document.getElementById('id1').value = "" + value + " ";
}


$(document).ready(function(e){

    $('#id1').bind('change keyup input', function(ev) {
        if(/\s/.test($(this).val())){
            // removes space
            this.value = this.value.replace(/[\s]/g, '');

            // submits ajax
            if(this.value.length>0)
                ajax_post();

            // clears input
            $('input[id=id1]').val('');
    }
});

这将调用您的函数

$('#id1').trigger("change");

通过编程更改值不会触发任何绑定事件。您可以使用
trigger
功能手动触发它们。

没问题-很乐意帮助:)