的document.getElementById(' BUTTON2&#39)。单击();不工作

时间:2016-03-09 13:39:45

标签: javascript

我尝试使用javascript" document.getElementById(' button2')自动触发子例程.Click();"因为它在下面的代码中。有人可以帮我看看为什么代码不起作用。谢谢

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" debug = "true"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Import Namespace="System.Data"%>
<script language="vb" runat="server">
Sub sendmsg(sender As Object, e As EventArgs)
response.Redirect("index.aspx")
End Sub
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function Confirm() {
    var str = $('#loutput').val(); //'We are inviting you for s special business meeting on Tueday, at No 12, Fred street. for more information please call 02341123333';

    var updateString = function(input_text) {
        //1. Find consecutive 11 numeric digits
        var match = input_text.match(/\d{11}/);
        //2. divide it into a string of 3s separated by space
        var new_str = '';
        for (var i = 1; i < match[0].length + 1; i++) {
            new_str = new_str + match[0][i - 1];
            if (i > 1 && i % 3 == 0)
                new_str = new_str + ' ';
        }
        //3. Replace old match no. with the new one
        console.log(match)
        if (match[0] != '')
            var confirm_value = document.createElement("INPUT");
        confirm_value.type = "hidden";
        confirm_value.name = "confirm_value";
        if (confirm("Message contains numeric characters which might make the message not delivered to some networks. Do you want us to reformat the message ?. This might increase the numbers of pages of the message and the cost?")) {
            confirm_value.value = "Yes";
            input_text = input_text.replace(match[0], new_str)
            $('#loutput').val(input_text);
            confirm2()

            //document.getElementById('loutput').innerHTML = input_text;

        } else {
            confirm_value.value = "No";
            confirm2()
        }
        $('#loutput').val(input_text);
        //document.getElementById('loutput').innerHTML = input_text;
    };
    updateString(str);
};

function confirm2() {
    var confirm_value = document.createElement("INPUT");
    confirm_value.type = "hidden";
    confirm_value.name = "confirm_value";
    if (confirm(" send the message now?")) {
        confirm_value.value = "Yes";
        document.getElementById('button2').click();

        //document.getElementById('loutput').innerHTML = input_text;

    } else {
        confirm_value.value = "No";
    }

    //document.getElementById('loutput').innerHTML = input_text;
};
</script>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">   </script>
<form runat="server" action="formatmessage3.aspx" method="post">
<input type="texarea" id="loutput" name="loutput" value="<%=request.form ("loutput") %>" />
<button ID="button1" OnClick="Confirm();" runat="server">Confirm</button>
<asp:button ID="button2" OnClick="sendmsg" Text="send" runat="server" />
</form>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

这里的问题是您使用带有runat =“server”的.NET按钮,因此它会将按钮的ID更改为其他值。

<asp:button ID="button2" OnClick="sendmsg" Text="send" runat="server" />

您必须通过类

引用元素
document.getElementsByClassName("myClass")[0].click();

或引用动态名称

document.getElementById("<%= button2.ClientID %>").click();

或在.NET4中,您可以使用ClientIDMode="Static"

答案 1 :(得分:-1)

.click()

jQuery function. 您只能在'jQueried'对象上使用它 因此:

$('#button2').click();
相关问题