jQuery / javascript |限制在contenteditable中的字符=" true"

时间:2014-12-19 09:19:53

标签: javascript jquery html ajax

我做过很多次尝试,但我不确定为什么这么多。 我知道之前有人提出限制性问题。我已经为解决方案实施了解决方案。

但是这个解决方案似乎有些错误。

http://stackoverflow.com/questions/2867479/limiting-number-of-characters-in-a-contenteditable-div

实际上它有效但有时它会在ajax帖子中发送总金额..

enter image description here

如果您在下面的图片中看到ajax帖子。

enter image description here

你可以看到它发布了不寻常的角色..

我怎么试过把限制只限于2个字符?

以下是以下代码。

var sum=0;
        //Now Need to Get Available Months on Based of Year
        $('tr.tableRow td.inner').on('click',function(e){
//            e.preventDefault();
//            e.stopImmediatePropagation();
            $(this).attr('contentEditable','true');
            $(this).addClass('inputCopyCat');
            $(this).focus();
            var lastColumnTotal = $(this).parent().find('.totalTD').text();
            var lastColumnSelector = $(this).parent().find('.totalTD');
//            console.log(lastColumn);
            max = 1;
            sum = Number(lastColumnTotal) - Number($(this).text());
            var editorSelector = $(this);
            editorSelector.keyup(function(e){
                $tr=$(this).closest('tr');
                $tr.find('td.inner').each(function(){
                    //console.log(Number($(this).text()));
                    sum += Number($(this).text());
                });
                //$tr.find('td.inner:last-child').text(sum);
                check_charcount(editorSelector, max, e);
            });
            editorSelector.keydown(function(e){ check_charcount(editorSelector, max, e); });

            function check_charcount(editorSelector, max, e)
            {
                if(e.which != 8 && editorSelector.text().length > max)
                {
                    // $('#'+content_id).text($('#'+content_id).text().substring(0, max));
                    e.preventDefault();
                    e.stopPropagation();
                    Parexons.notification('You can Not Add More Characters','warning');
                }
            }
        });
        $('tr.tableRow td.inner').focusout(function(e){
//            e.preventDefault();
//            e.stopImmediatePropagation();
            $('td.inputCopyCat').removeAttr('contentEditable');
            $('td.inputCopyCat').removeClass('inputCopyCat');
            var hoursWorked = Number($(this).text());
            var dateOfHoursWorked = $(this).attr('td-data');
            var projectID = $( 'td:first-child', $( this ).parents ( 'tr' ) ).attr('data-project');
            var lastColumnSelector = $(this).parent().find('.totalTD');
            console.log(hoursWorked);
            var postData = {
              hoursWorked: hoursWorked,
                dateOfHoursWorked: dateOfHoursWorked,
              projectID: projectID
            };
            $.ajax({
                url: "<?php echo base_url(); ?>leave_attendance/updateTimeSheetInfo",
                data: postData,
                type: "POST",
                success:function(output){
                    var data = output.split("::");
                    if(data[0] === "OK"){
                        Parexons.notification(data[1],data[2]);
                        //Now Need to Sum The Values in the Total if Record Return is Success...
                        totalSum = sum + Number(hoursWorked);
                        //console.log(sum);
                        lastColumnSelector.html(sum);
                    }else if(data[1] === "FAIL"){
                        Parexons.notification(data[1],data[2]);
                    }
                }
            });
        });

我试图创建相同的小提琴但我无法在小提琴中找到这个问题,你的JQuery代码是一样的。它真的很混乱。 我的意思是在当地它也可以工作,但即使你有一个限制,有时发送不寻常的性格。但是对于jsFiddle,我似乎很好。和代码差不多。

JS FIDDLE LINK HERE

有没有办法在本地解决问题?

0 个答案:

没有答案
相关问题