-1

如何在不使用 javascript 或 jquery 重定向页面的情况下更新表这是我的脚本

    function doubleclick(table,id1)
        {
            table.innerHTML="<input type=text name=tab onBlur=\"javascript:submitNewName(this,'"+id1+"');\" value=\""+table.innerHTML+"\">";
            table.firstChild.focus();

        }
        function submitNewName(text,id2)
        {
            text.parentNode.innerHTML=text.value;
        $.ajax({
            url: 'update',
            data:{text1:text.value,id:id2},
            type:"POST",
            dataType:"json",
            error:function(data)
            {
                alert('error')
            },
            success: function(data) 
            {
               // alert('updated!')
            }
        });  
        }

html

<td onDblclick="javascript:doubleclick(this,${item.id});">${item.filedesc}</td>

当我双击表格时,列内将出现一个编辑框,当文本框失去焦点(通过单击表格外部)时调用 onblur 事件,当它失去焦点时,应调用更新 servlet 而无需重定向页面。更新过程应该在后台发生

编辑

抱歉编辑迟了。对我的脚本进行了一些编辑。我已经发布了我完整的工作 html 和 javascript 代码。一个月前解决了这个问题。用户请检查代码并建议是否可以进一步改进。

4

3 回答 3

1

您可以使用 jquery 的 ajax 来实现这一点。您可以传递一个回调方法,以便在真正的更新完成时通知您。

这是一个表格单元格,它可以是任何东西,我在这个例子中使用了 div:

<div id="table-cell" editing="0">Value</div>    

js应该是这样的

$('#table-cell').dblclick(function(){
    if ($(this).attr('editing') == '0')
    {
        // only trigger this when the cell is not in edit mode
        $(this).attr('editing',1);

        // show the input
        var input = $('<input type="text" value="'+$(this).html()+'">');
        $(this).html('');
        $(this).append(input);

        // bind blur event to input
        input.blur(function(){

            // update value from input to the table cell
            var value = $(this).val()
            $(this).parent().html(value);
            $(this).parent().attr('editing',0);

            // send the data to the server for update
            $.ajax({
                url: '/save_data/',
                data: {data:value},
                type:"POST",
                dataType:"json",
                error:function(data){alert('error')},
                success: function(data) {
                    alert('updated!')
                }
            });
        });
    }
});
于 2012-08-22T10:10:46.800 回答
0

使用 $.post 速记方法。

看看http://api.jquery.com/jQuery.post/

于 2012-08-22T10:00:11.607 回答
0

看看 jQuery 中的 $.ajax() 方法。在不触发页面刷新的情况下检索或发布内容非常简单。

http://api.jquery.com/jQuery.ajax/

于 2012-08-22T10:01:44.377 回答