如何在javascript中的gridview中获取新输入的值

Posted

技术标签:

【中文标题】如何在javascript中的gridview中获取新输入的值【英文标题】:how to get the new entered value in gridview in javascript 【发布时间】:2018-01-22 07:54:02 【问题描述】:

我正在处理一个项目并使用网格视图来查看一些可编辑的数据。我对这些数据有一些限制,我想在数据发送到数据库之前执行前端检查。我的代码如下:

<asp:Button ID="Update" runat="server" CausesValidation="True" 
            CommandName="Update" Text="Update" onclientClick="check(this)"/>

而我的 javascript 如下:

function check(up) 

    var ip, desc, rt;

    var table = document.getElementById('<%=GridView1.ClientID %>');

    var row = up.parentNode.parentNode;

    ip = row.cells[1].children[0].attributes[2].value;
    desc = row.cells[2].children[0].attributes[2].value;
    rt = row.cells[3].children[0].attributes[2].value;
    time = parseFloat(rt);
    alert(rt);

    if (ip === null || ip.match(/^ *$/) !== null) 
        alert('please enter IP Address');
        return false;
    
    else if (desc === null || desc.match(/^ *$/) !== null) 
        alert('please enter description');
        return false;
    
    else if (rt === null || rt.match(/^ *$/) !== null) 
        alert('please enter refresh time');
        return false;
    
    else if (isNaN(time))
        alert('refresh time must be a number');
        return false;
    
    else if (Number(time) < 1) 
        alert('the minimum refresh time is 1');
        return false;
    
    else 
        return true;

问题是我总是得到旧值而不是新值。如何在 JavaScript 中获取新值? 我在更新查询中使用sqlDataSource

UPDATE Clients
SET ipAddress = @ipAddress, description = @description, refreshTime = @refreshTime
WHERE machineName = @MachineName

【问题讨论】:

【参考方案1】:

你没有从你的行获取行索引,试试这个:

var table = document.getElementById('<%=GridView1.ClientID %>');

var row = up.parentNode.parentNode;

var rindex = row.rowIndex; // get gridview row index

ip = table.rows[rindex].cells[1].children[0].value;
desc = table.rows[rindex].cells[2].children[0].value;
rt = table.rows[rindex].cells[3].children[0].value;

【讨论】:

以上是关于如何在javascript中的gridview中获取新输入的值的主要内容,如果未能解决你的问题,请参考以下文章

如何在javascript中的gridview中获取新输入的值

如何使用javascript函数在GridView中查找文本框,该函数在同一GridView RSS中的一个radcombobox的“Onclientsideselectedindexchanged”

Android GridView 焦点顺序

如何使用javascript在asp.net中gridview内的按钮单击上应用文本框空白验证?

Gridview中的JQuery如何在文本框在同一行更改时检查复选框

如何在不同 jquery ui 选项卡中的 gridviews 中使用 jquery Datatable 插件?