当单击 ImageButton
时,我想使用 java 脚本代码在 TextBox
中插入所需的单词。 TextBox
在插入所需单词之前可能有一些文本。我使用了下面的 VB.NET 代码,它工作正常,但我想在客户端 seide 中进行。这怎么可能? (我是 java 脚本的新手)
Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
Dim emoticsign As String = TextBox2.Text & ":)"
ScriptManager.RegisterStartupScript(Me.Page, Me.Page.[GetType](), "myScript", "document.getElementById('" + TextBox2.ClientID & "').value = ' " & emoticsign & "';", True)
SetFocus(TextBox2)
End Sub
最佳答案
只需通过 id 定位元素并设置值。
Javascript
function insertText() {
document.getElementById("Hello").value = "This is inserted";
}
.aspx
<asp:TextBox runat="server" ID="Hello"></asp:TextBox>
<asp:ImageButton runat="server" Text="Insert" OnClientClick="insertText()" />
如果你想处理已经在这个文本框中的文本并只是附加到它,那么你可以添加一个 if/else
,比如:
function insertText() {
var textbox = document.getElementById("Hello")
if(textbox.value != "") {
textbox.value = textbox.value + " text appended";
textbox.focus();
}
else {
textbox.value = "This is inserted";
textbox.focus();
}
}
关于javascript - 在 Asp.net 的 TextBox 中插入所需文本的 java 脚本代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36988643/