0

我的函数 valueS() 由于某种原因不起作用,它不会触发底部的 ajax 函数......由于某种原因 bind('change keyup input') 在添加空格时不会触发。

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

$(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('');
    }
});
4

1 回答 1

1

这将调用您的函数...

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

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

于 2013-10-21T16:29:30.030 回答