【问题标题】:How to retrieve cell content of textbox when a button is clicked in jqGrid?在jqGrid中单击按钮时如何检索文本框的单元格内容?
【发布时间】:2014-01-09 07:04:06
【问题描述】:

在我的 jqGrid 中,我有一个文本框和一个按钮。单击该按钮时,我需要保存用户键入的文本框的值。我试过了,但我没有得到输出。

以下是我的colModel

{ name: 'Price', index: 'Price', width: 120, editable: true, edittype: "text", formatter: generateTextBox },
{ name: 'Checkout', index: 'Checkout', width: 120, editable: true, formatter: generateCheckoutBtn }

我已经编写了自定义格式化程序来生成文本框和按钮,代码如下,

function generateTextBox(cellvalue, options, rowObject) {
        return '<input type="text" size="10" maxlength="15" />';
    }

然后我写了一个函数来检索文本框的值,并在 generateCheckoutBtn 格式化程序中的 onclick 中调用它,如下

function generateCheckoutBtn(cellvalue, options, rowObject) {
        return '<button onclick="return getTextBoxValue(' + rowObject.Id + ')">Checkout</button>';
    }

getTextBoxValue函数,如下

function getTextBoxValue(SelectedId) {            
        alert("RowId: " + SelectedId);
        var txt = jQuery("#CheckList").jqGrid('getCell', SelectedId, 'Price');
        alert("" + txt);
    }

该函数正确返回rowId,使用警报检查,但它返回txt的警报,

<input type="text" size="10" maxlength="15" />

我不知道如何获取用户在文本框中输入的单元格内容,然后将其保存到数据库中。当用户点击 Checkout 按钮而不输入文本时,我必须发出 alert

任何帮助将不胜感激!

【问题讨论】:

  • Fiddle 可以很好地解决这个问题。

标签: javascript jquery jqgrid


【解决方案1】:

对于输入,也许你可以给它一个名字...

<input type="text" size="10" maxlength="15" name="yourtxtboxname" />

然后使用名称传递它。

【讨论】:

    【解决方案2】:

    您必须使用唯一的 ID 区分每个单独的文本框。将文本框 id 与格式化程序中的 rowId 连接起来:

    function generateTextBox(cellvalue, options, rowObject) {
        return "<input type=\"text\" size=\"10\" maxlength=\"15\" id=\"textbox" + options.rowId + "\"/>";
    }
    

    然后在检索文本框内的值时使用常规选择器:

    function getTextBoxValue(SelectedId) {            
        alert("RowId: " + SelectedId);
        var txt = $("#textbox" + SelectedId).val();
        alert("" + txt);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      • 2016-01-12
      • 2013-08-13
      • 2019-04-25
      • 2015-11-16
      • 1970-01-01
      相关资源
      最近更新 更多