【问题标题】:redirect to servlet class file using jquery使用 jquery 重定向到 servlet 类文件
【发布时间】:2015-03-03 09:48:07
【问题描述】:

我用过 jquery-1.11.2.min.js 和 chrome 40.0.2214.115

在用户注册表单中,如果任何字段为空,我想向用户显示警报消息,否则重定向到 servlet 类文件,在该 servlet 类中,我编写了用于插入用户详细信息的 db 连接。

我的用户注册html代码是

    <script type="text/javascript" src="js/plugins/jquery-1.11.2.min.js"></script>
<script>
$(document).ready(function()
        {
    $("#submitid").click(function()
      {
        var fname=$("#firstname2").val();
        var lanme=$("#lastname2").val();
        var email=$("#email2").val();
        var mobileno=$("#mobileno").val();
        var username=$("#username").val();
        var pass=$("#password").val();
        if(fname==''||lname==''||email=''||mobileno==''||username==''||pass=='')
            {
            alert("Enter all fields");
            $("#userform").focus();

            }
        else
            window.location.href("Userdetailsins.java");
            });
        });</script>
</head>

<body>

<form class="stdform stdform2" method="post"  id="userform">
<label>First Name</label>
 <input type="text" name="userfname" id="firstname2" class="longinput" />
<label>Last Name</label>
<input type="text" name="userlname" id="lastname2" class="longinput" />
<label>Email</label>
<input type="text" name="mailid" id="email2" class="longinput" 
<label>Mobile no</label>
 <input type="text" name="mobno" id="mobileno" class="longinput" 
 <label>User name </label>
 <input type="text" name="username" id="username" class="longinput" />
 <label>User Password </label>
 <input type="password" name="password" id="password" class="longinput" /></span>                     
 <input type="reset" value="Reset Button" />
 <input type="submit"  id="submitid" name="submitid" value="Next"/>
  </form>
</body>
</html>

当字段为空并且不重定向到另一个表单时,它不会显示警报消息。即使我尝试使用另一个 html 文件来重定向它也不起作用。对于重定向,我遵循了如下所示的准则

//$(location).attr('href',"http://www.google.com");
        //window.open("next.html");
        //$("#submitform").focus();

         //$(window.location).attr('href', 'http://www.google.com');

        //window.navigator("next.html");
        //self.location="next.html";

    //top.location="next.html";
    //var url="www.google.com"
        //window.location.replace(url);

    //window.location.href("next.html");
    window.open("next.html");

window.open() 方法有效,但我不想作为单独的窗口打开,我只是为了重定向。请任何人帮助解决这个问题。

谢谢....

【问题讨论】:

  • 通过使用这个你得到任何错误 window.location.href("next.html"); @Asha
  • Thanks@coding cracker 我没有收到任何错误。位于同一页面上的表单未重定向到 next.html。
  • 点击后你需要重定向到另一个页面吗?
  • 试试我的答案,它会起作用的:)

标签: jquery servlets redirect


【解决方案1】:

使用这个

function nextredirect(){
  window.location.href = 'next.html';
}

jQuery

$('#submitid').click(function(){

if($('#firstname2').val() == ""){
alert('Please enter the first name');
}else{
 window.location.href = 'next.html';
}
});

HTML

 <input type="button"  id="submitid" name="submitid" value="Next" onclick="nextredirect()"/>

【讨论】:

  • 感谢它在 onclick 事件中起作用。但我想使用 jquery 验证表单然后只有我会重定向到下一页...... jquery 有什么办法吗? @编码破解者
  • 很高兴为您提供帮助 :) @Asha
  • 另外一个请求编码破解程序,当使用输入类型作为按钮但输入类型=“提交”时,您的代码可以正常工作,但表单不会重定向。请帮忙解决这个问题...@coding cracker
  • $("#here your submit button id").submit(function(e){ return false; });
  • 否则你可以 $('#formid').submit 而不是 window.location.href = 'next.html' 这个 Asha 明白了吗?
猜你喜欢
  • 2014-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-02
  • 1970-01-01
  • 1970-01-01
  • 2018-04-18
  • 1970-01-01
相关资源
最近更新 更多