gpt4 book ai didi

javascript - 单击按钮可更改动态生成的焦点文本框中的文本

转载 作者:行者123 更新时间:2023-11-28 07:20:54 25 4
gpt4 key购买 nike

我正在使用 VS2013 创建一个网站来收集平板电脑上的一些数据(使用 IE)。我不想使用平板电脑键盘,所以我用 asp 按钮创建了一个数字键盘。我需要知道哪个文本框具有焦点,以便将文本更改为单击的按钮的值。

这是我的问题 - 文本框是在 gridview 模板字段内动态创建的,该字段也是在每行数据绑定(bind)时动态创建的。因此该字段和控件在 aspx 页面中不存在。

我不知道如何确定在单击按钮之前选择哪个文本框。我相信这需要在客户端使用 js 或 jquery 完成,但我以前从未使用过这些。

我需要一个脚本,将单击的数字插入最近选择的文本框中。

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If (Not IsPostBack) Then
tfield = New TemplateField()
tfield.HeaderText = "Count"
GridView1.Columns.Add(tfield)

Dim GhostTable As New DataTable
GhostTable.Columns.Add(New DataColumn("Count"))

'Initialize table with 1 blank row
Dim blankRow As DataRow = GhostTable.NewRow()
GhostTable.Rows.Add(blankRow)

'Makes the gridview reflect the GhostTable (currently 1 blank row)
GridView1.DataSource = GhostTable
GridView1.DataBind()
Else
'Occurs on every postback
GridView1.DataSource = Session("GhostTable")
GridView1.DataBind()
End If
End Sub

Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
'Handles databinding of each gridview1 row and adds the textboxes to the template fields
If e.Row.RowType = DataControlRowType.DataRow Then
Dim tbxCount As New TextBox()
tbxCount.ID = "tbxCount"
tbxCount.Text = TryCast(e.Row.DataItem, DataRowView).Row.Item("Count").ToString()
tbxCount.Width = 100
tbxCount.Height = 61
tbxCount.Font.Size = 36
e.Row.Cells(5).Controls.Add(tbxCount)
End if
End sub

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Determine which of the gridview row's "tbxCount" was focused and insert the number 1
'preferably this would happen on the client side?
End Sub

抱歉大家拖了这么久。只是想确保我提供了足够的信息。

最佳答案

这是一个独立的示例,说明如何处理文本框的 onfocus 事件,在事件发生时将文本框分配给变量,以及在单击按钮时操作最后一个获得焦点的文本框的内容。

var lastTbox;在页面加载时定义一个全局变量,因此它不在函数内。

<html><body>
<script>

var lastTbox;

function focusFunction(val){

lastTbox = val;
}

function buttonClick(){

lastTbox.value = "text";
}

</script>

<input type="text" id="tbox1" onfocus="focusFunction(this)">
<input type="text" id="tbox2" onfocus="focusFunction(this)">
<input type="button" onclick="buttonClick()">

</body></html>

关于javascript - 单击按钮可更改动态生成的焦点文本框中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30331595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com