使用document ready重定向不起作用

redirect using document ready does not work

本文关键字:不起作用 重定向 ready document 使用      更新时间:2023-09-26

下面是我的代码,我似乎找不到什么问题,它没有重定向到链接。

<html>
<head>
    <script type="text/javascript" src="<?= baseurl(); ?>/public/js/jquery-1.9.1.min.js"></script>        
    <script type="text/javascript">            
        $(document).ready(function() {
            $(function() { $("formRedirect").submit(); });
        });
    </script>
</head>
<body>
    <form id='formRedirect' action="http://www.example.com/something/something.aspx" method="post"><input type="submit" name="redirect" value='<?= $token ?>'</form> 
</body>

试试这个:

<html>
<head>
    <script type="text/javascript" src="<?= baseurl(); ?>/public/js/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">            
        $(document).ready(function() { 
         $("#formRedirect").submit(); 
        }); 
    </script>
</head>
<body>
    <form id='formRedirect' action="http://www.example.com/something/something.aspx" method="post"><input type="submit" name="redirect" value='<?= $token ?>'</form> 
</body>

为id jquery检测它们的#

试试这样

$("#formRedirect")[0].submit();
使用

$(document).ready(function() {});

$(function(){})

两个相同的

您在document.ready中添加了$(function(){});

$(function(){});$(document).ready(function() {});相似

使用以下代码:

$(document).ready(function() { 
 $("#formRedirect").submit(); 
}); 

我发现Chrome控制台在调试这种情况时非常有用。我将首先确保您从jQuery选择器获得一个结果。

你可以打开Chrome控制台按F12

在Chrome控制台中,输入$("formRedirect"),看看是否得到任何结果。您可能不会,因为您缺少# ID选择器。

接下来,尝试提交修改后的jQuery对象。如果它不起作用,尝试通过在选择器的末尾添加[0]来访问实际的HTML对象,如@Anik所建议的。