Javascript 重定向JQuery

Javascript 重定向JQuery,javascript,jquery,html,Javascript,Jquery,Html,我做了以下代码: 文件index.php <html> <head> <title>Jeu de dammes</title> </head> <style> form { margin-left:500px;margin-top:300px; width:300px } </style> <body> <form id="formulaire" style="margin-left:10

我做了以下代码: 文件index.php

<html>
<head>
<title>Jeu de dammes</title>
</head>
<style>
form { margin-left:500px;margin-top:300px;  width:300px }
  </style>
<body>
<form id="formulaire" style="margin-left:100px, padding: 15px" >
<table>
<tr><td><h4> The first player</h4></td>
    <td><input   type="text"   id="premier"></td>
</tr>
<tr><td><h4> The second player</h4></td>
    <td><input   type="text"   id="deuxieme"></td>
</tr>
<tr>
<td></td>
<td>
<button   id="action">Play</button></td>
</tr>
</table>
</form>
<script   src="jquery-1.5.1.js"></script>
<script>

    $(function () {

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

            var j1= $('#premier').val();
            var j2= $('#deuxieme').val();

            if( j1=="") alert('saisir le nom du premier joueur');
            if( j2=="") alert('saisir le nom du deuxieme joueur');
            if(j2!="" && j1!="") {$(location).attr('href',"http://www.google.com");}
        });

    $('body').css('background-image', 'url(flower2.jpg)').css('background-size', '100%');
    $("#formulaire").css('background-image', 'url(flower.jpg)');
    });
</script>
</body>
</html>

Jeu de dammes
表格{左边距:500px;右边距顶部:300px;宽度:300px}
第一名选手
第二名选手
玩
$(函数(){
$(“#操作”)。单击(函数(){
var j1=$('#premier').val();
变量j2=$('#deuxieme').val();
如果(j1==“”)警报(“总理之名”);
如果(j2==“”)警报('saisir le nom du deuxieme jouer');
如果(j2!=“”&&j1!=“”){$(位置).attr('href',”http://www.google.com");}
});
$('body').css('background-image','url(flower2.jpg')).css('background-size','100%);
$(“#formulaire”).css('background-image','url(flower.jpg');
});
我的问题是,如果单击重定向到“www.google.com”不起作用。有任何建议:

  • 这个错误的原因是什么
  • 我怎样才能纠正它

  • 无需使用jQuery来做一些vanilla JavaScript已经做过的事情:

    window.location.href = "http://www.google.com/";
    
    它不起作用,可能是因为您使用的是一个表单,它在您可以重定向之前提交。捕获
    submit
    事件:

    将您的代码更改为此,它将工作:

    $('#formulaire').submit(function(e) {
        var j1 = $('#premier').val();
        var j2 = $('#deuxieme').val();
    
        if (j1 == "") {
            alert('saisir le nom du premier joueur');
        }
    
        if (j2 == "") {
            alert('saisir le nom du deuxieme joueur');
        }
    
        if (j2 && j1) {
            window.location.href = "http://www.google.com";
        }
    
        e.preventDefault();
    });
    

    要重定向,只需更改
    位置
    全局变量:

    location = "http://www.google.com"
    
    您所做的工作不起作用,因为您通常只是将选择器或回调传递给jQuery函数
    $(…)
    (例如
    $(“#某物a”)
    位置是JavaScript提供的全局变量

    更换您的

    $(location).attr('href',"http://www.google.com");
    
    使用简单的Javascript

    window.location = "http://www.google.com"
    
    编辑:

    好的,我的错,这是一个附加的问题,当表单出现问题时,您不会返回false,也不会在执行重定向时返回false,因此表单(POST)的deafult操作会打破以前的导航窗口。location

    当你打开页面时,当其中一个字段没有被填充时,只需在上发布一个演示


    您可以在

    jQuery中看到代码mod也可以包装对象;“按”位置=“”“但是同样的事情@JanDvorak我想不是,但我试图表明他需要一个实际的URL,而不仅仅是主机名。建议两次并不能解决问题。前者同样有效。@JanDvorak你说得对!还有一个问题,刚刚编辑了我的答案。请现在试试。那么,没有警报和重定向?控制台中有日志吗?@lamloumi:重定向到谷歌将不起作用。谷歌似乎阻止了JS重定向。@JanDvorak:Framebuster。JSFIDLE以
    格式显示网站,大多数大型网站都试图打破这种模式。