简体   繁体   中英

document.getElementById('button2').click(); not working

I am try to automatically fire a sub routine with with a javascript "document.getElementById('button2').click();"as it is in the below code. Can someone please help me see why the code is not working. Thank you

<%@ 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>

The problem here is the fact you are using a .NET button with runat="server" so it is going to change the id of the button to a different value.

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

You either have to reference the element by a class

document.getElementsByClassName("myClass")[0].click();

or reference the dynamic name

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

or in .NET4 you can use ClientIDMode="Static"

.click()

Is a jQuery function.
You can only use it on 'jQueried' objects.
Hence:

$('#button2').click();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM