我试图制作一个允许笑脸的留言簿。我已经让它解析诸如[smile]等之类的东西,并用图像替换它,但是我无法做一件事。

当用户单击一个笑脸时,我希望它在文本框中插入正确的标签,但它不会执行任何操作。我曾经使用JavaScript尝试插入标签。

见链接:

http://www.vittoriosastarsnursery.com/guestsign.aspx

这是代码:

<%@ Page Explicit="True" Language="VB" Debug="True" ValidateRequest="false" %>

<%@ Register TagPrefix="aspHut" TagName="templateTop" Src="guesttop.ascx" %>
<%@ Register TagPrefix="aspHut" TagName="templateBottom" Src="guestbottom.ascx" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
    Dim strConnection As String = "provider=Microsoft.Jet.OLEDB.4.0;data source=" + Server.MapPath("guestbook.mdb.resources")

    Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
        If (Page.IsValid) Then
            ' using .Replace("'", "") to remove naughty single quote
            Dim sName As String = Server.HtmlEncode(txtName.Text.Replace("'", ""))
            Dim sEmail As String = Server.HtmlEncode(txtEmail.Text.Replace("'", ""))
            Dim sMessage As String = Server.HtmlEncode(txtMessage.Text.Replace("'", ""))

            Dim objConnection As OleDbConnection
            Dim objCmd As OleDbCommand
            Dim strSQL As String = "INSERT INTO messages (name, email, message) VALUES ('" & sName & "', '" & sEmail & "', '" & sMessage & "')"

            objConnection = New OleDbConnection(strConnection)
            objConnection.Open()

            objCmd = New OleDbCommand(strSQL, objConnection)
            objCmd.ExecuteNonQuery()

            objConnection.Close()
            Response.Redirect("Guestbook.aspx")
        Else
            Exit Sub
        End If
    End Sub
</script>
<aspHut:templateTop ID="UserControl1" runat="server"></aspHut:templateTop>
<script language="JavaScript">
    var bodytext = "";
    function AddText(NewCode) {
        var dob = document.formSign.txtMessage
        if (dob.createTextRange && dob.caretPos) {
            var caretPos = dob.caretPos;
            caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? NewCode + ' ' : NewCode;
        }
        else if (dob.setSelectionRange) {
            dob.value = dob.value.substring(0, dob.selectionStart) + NewCode + dob.value.substring(dob.selectionEnd, dob.value.length);
        }
        else {
            dob.value += NewCode;
        }
        dob.focus();
        bodytext = "";
    }
</script>
<form id="formSign" runat="server">
<p>
    Please enter the following requested information below to sign the guestbook.
    <table>
        <tr>
            <td align="left">
                <b>Your Name:</b><asp:requiredfieldvalidator id="nameReqVal" runat="server" controltovalidate="txtName"
                    errormessage="Your Name.">*</asp:requiredfieldvalidator>
            </td>
            <td align="right">
                <asp:textbox id="txtName" runat="server"></asp:textbox>
            </td>
        </tr>
        <tr>
            <td align="left">
                <b>Your Email Address:</b><asp:regularexpressionvalidator id="emailRegEx" runat="server"
                    controltovalidate="txtEmail" errormessage="Your Email." validationexpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:regularexpressionvalidator>
            </td>
            <td align="right">
                <asp:textbox id="txtEmail" runat="server"></asp:textbox>
            </td>
        </tr>
        <tr align="left">
            <td colspan="3">
                <b>Your Message:</b><asp:requiredfieldvalidator id="messageReqVal" runat="server"
                    controltovalidate="txtMessage" errormessage="Your Message.">*</asp:requiredfieldvalidator>
            </td>
        </tr>
        <tr align="left">
            <td colspan="2">
                <asp:textbox id="txtMessage" runat="server" textmode="MultiLine" columns="50" rows="10"></asp:textbox>
            </td>
            <td>
                <table>
                    <tr>
                        <td>
                            <a href="javascript:void(AddText('[angel]'))">
                                <img src="/Images/Smileys/angel_smile.gif" border="0" height="22" width="22" /></a>
                        </td>
                        <td>
                            <a href="javascript:void(AddText('[angry]'))">
                                <img src="/Images/Smileys/angry.gif" border="0" height="22" width="22" /></a>
                        </td>
                        <td>
                            <a href="javascript:void(AddText('[teeth]'))">
                                <img src="/Images/Smileys/baring_teeth.gif" border="0" height="22" width="22" /></a>
                        </td>
                        <td>
                            <a href="javascript:void(AddText('[confused]'))">
                                <img src="/Images/Smileys/confused.gif" border="0" height="22" width="22" /></a>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <a href="javascript:void(AddText('[cry]'))">
                                <img src="/Images/Smileys/cry.gif" border="0" height="22" width="22" /></a>
                        </td>
                        <td>
                            <a href="javascript:void(AddText('[dunno]'))">
                                <img src="/Images/Smileys/dont_know.gif" border="0" height="22" width="22" /></a>
                        </td>
                        <td>
                            <a href="javascript:void(AddText('[ssh]'))">
                                <img src="/Images/Smileys/dont_tell.gif" border="0" height="22" width="22" /></a>
                        </td>
                        <td>
                            <a href="javascript:void(AddText('[haha]'))">
                                <img src="/Images/Smileys/hahaha.gif" border="0" height="22" width="22" /></a>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <a href="javascript:void(AddText('[nerd]'))">
                                <img src="/Images/Smileys/nerd.gif" border="0" height="22" width="22" /></a>
                        </td>
                        <td>
                            <a href="javascript:void(AddText('[omg]'))">
                                <img src="/Images/Smileys/omg.gif" border="0" height="22" width="22" /></a>
                        </td>
                        <td>
                            <a href="javascript:void(AddText('[party]'))">
                                <img src="/Images/Smileys/party.gif" border="0" height="22" width="22" /></a>
                        </td>
                        <td>
                            <a href="javascript:void(AddText('[red]'))">
                                <img src="/Images/Smileys/red.gif" border="0" height="22" width="22" /></a>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <a href="javascript:void(AddText('[smile]'))">
                                <img src="/Images/Smileys/regular_smile.gif" border="0" height="22" width="22" /></a>
                        </td>
                        <td>
                            <a href="javascript:void(AddText('[rolleyes]'))">
                                <img src="/Images/Smileys/roll.gif" border="0" height="22" width="22" /></a>
                        </td>
                        <td>
                            <a href="javascript:void(AddText('[sad]'))">
                                <img src="/Images/Smileys/sad_smile.gif" border="0" height="22" width="22" /></a>
                        </td>
                        <td>
                            <a href="javascript:void(AddText('[wink]'))">
                                <img src="/Images/Smileys/wink_smile.gif" border="0" height="22" width="22" /></a>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <a href="javascript:void(AddText('[sarcastic]'))">
                                <img src="/Images/Smileys/sarcastic.gif" border="0" height="22" width="22" /></a>
                        </td>
                        <td>
                            <a href="javascript:void(AddText('[shades]'))">
                                <img src="/Images/Smileys/shades.gif" border="0" height="22" width="22" /></a>
                        </td>
                        <td>
                            <a href="javascript:void(AddText('[what]'))">
                                <img src="/Images/Smileys/what_smile.gif" border="0" height="22" width="22" /></a>
                        </td>
                        <td>
                            <a href="javascript:void(AddText('[sick]'))">
                                <img src="/Images/Smileys/sick.gif" border="0" height="22" width="22" /></a>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <a href="javascript:void(AddText('[sleepy]'))">
                                <img src="/Images/Smileys/sleepy.gif" border="0" height="22" width="22" /></a>
                        </td>
                        <td>
                            <a href="javascript:void(AddText('[grin]'))">
                                <img src="/Images/Smileys/teeth_smile.gif" border="0" height="22" width="22" /></a>
                        </td>
                        <td>
                            <a href="javascript:void(AddText('[thinking]'))">
                                <img src="/Images/Smileys/thinking.gif" border="0" height="22" width="22" /></a>
                        </td>
                        <td>
                            <a href="javascript:void(AddText('[tongue]'))">
                                <img src="/Images/Smileys/tongue_smile.gif" border="0" height="22" width="22" /></a>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr align="left">
            <td colspan="3">
                <asp:validationsummary id="ValSum" runat="server" width="312px" headertext="Please correct the following error(s):"></asp:validationsummary>
                <asp:button id="btnSubmit" onclick="btnSubmit_Click" runat="server" text="Sign!"></asp:button>
            </td>
        </tr>
    </table>
</form>
<p align="center">
    <a href="guestbook.aspx">View the guestbook</a></p>
<aspHut:templateBottom ID="UserControl2" runat="server"></aspHut:templateBottom>

最佳答案

var bodytext = "";
function AddText(NewCode) {
    var dob = document.getElementById('txtMessage'); // just changed this line ;)
    if (dob.createTextRange && dob.caretPos) {
        var caretPos = dob.caretPos;
        caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? NewCode + ' ' : NewCode;
    }
    else if (dob.setSelectionRange) {
        dob.value = dob.value.substring(0, dob.selectionStart) + NewCode + dob.value.substring(dob.selectionEnd, dob.value.length);
    }
    else {
        dob.value += NewCode;
    }
    dob.focus();
    bodytext = "";
}


为我工作:)

10-07 23:11