Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery/javascript“限制contenteditable中的字符数=”;“真的”;_Javascript_Jquery_Html_Ajax - Fatal编程技术网

jQuery/javascript“限制contenteditable中的字符数=”;“真的”;

jQuery/javascript“限制contenteditable中的字符数=”;“真的”;,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,我做了很多尝试,但我不知道为什么这是如此多的马车。 我知道以前有人问过这个问题。我已经为他们实现了解决方案 但这一解决方案似乎有问题 http://stackoverflow.com/questions/2867479/limiting-number-of-characters-in-a-contenteditable-div 实际上它是有效的,但有时它会在ajax帖子中发送总量 如果您在下图中看到ajax帖子 你可以看到它正在发布不寻常的字符 我是如何尝试将限制仅限于2个字符的 下面是代

我做了很多尝试,但我不知道为什么这是如此多的马车。 我知道以前有人问过这个问题。我已经为他们实现了解决方案

但这一解决方案似乎有问题

http://stackoverflow.com/questions/2867479/limiting-number-of-characters-in-a-contenteditable-div
实际上它是有效的,但有时它会在ajax帖子中发送总量

如果您在下图中看到ajax帖子

你可以看到它正在发布不寻常的字符

我是如何尝试将限制仅限于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]);
                    }
                }
            });
        });
var总和=0;
//现在需要根据年份获得可用的月份
$('tr.tableRow td.inner')。在('click',函数(e)上{
//e.预防违约();
//e.立即停止传播();
$(this.attr('contentEditable','true');
$(this.addClass('inputCopyCat');
$(this.focus();
var lastColumnTotal=$(this.parent().find('.totalTD').text();
var lastColumnSelector=$(this.parent().find('.totalTD');
//console.log(最后一列);
max=1;
sum=Number(lastColumnTotal)-Number($(this.text());
var editorSelector=$(此项);
编辑选择键控(功能(e){
$tr=$(this.closest('tr');
$tr.find('td.inner')。每个(函数(){
//log(编号($(this.text());
总和+=数字($(this).text());
});
//$tr.find('td.inner:last child')。文本(总和);
检查字符数(编辑选择器,最大值,e);
});
editorSelector.keydown(函数(e){check_charcount(editorSelector,max,e);});
函数检查\u字符数(编辑器选择器,最大值,e)
{
if(e.which!=8&&editorSelector.text().length>max)
{
//$('#'+content_id).text($('#'+content_id).text().substring(0,max));
e、 预防默认值();
e、 停止传播();
通知('不能添加更多字符','warning');
}
}
});
$('tr.tableRow td.inner').focusout(函数(e){
//e.预防违约();
//e.立即停止传播();
$('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');
控制台日志(工作小时);
var postData={
工作时间:工作时间,
Hoursworked的日期:Hoursworked的日期,
projectd:projectd
};
$.ajax({
url:“请假/更新信息”,
数据:postData,
类型:“POST”,
成功:功能(输出){
var data=output.split(“:”);
如果(数据[0]=“正常”){
Parexons.通知(数据[1],数据[2]);
//如果记录返回成功,现在需要求和总数中的值。。。
总和=总和+数量(工作小时);
//控制台日志(总和);
html(sum);
}否则如果(数据[1]=“失败”){
Parexons.通知(数据[1],数据[2]);
}
}
});
});
我曾尝试创建相同的fiddle,但我在fiddle中找不到这个问题,因为JQuery代码是相同的。??真让人困惑。 我的意思是,它在本地也能起作用,但有时会发送不寻常的字符,即使有限制。但在JSFIDLE上,我觉得很好。代码几乎是一样的


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

为什么不使用简单的文本输入?这是一个表格。。如何在按下和焦点不在时使用表td内的输入删除输入。但这不是问题所在,即使是在文本输入上,我也必须使用limit。代码几乎是一样的。问题可能恰恰在于它们之间的差异。