window.location.assign("link"),无效

Swa*_*dra 1 html javascript css

这是javascript代码.

<script type="text/javascript">
        function myfunc()
        {
            var filmName = document.getElementById("mysearch-text").value;
            alert(filmName);
            if(filmName == "parker")
                 window.location.assign("http://www.google.com");
            else
                alert("hello");

        }       
    </script>
Run Code Online (Sandbox Code Playgroud)

如果我输入任何其他字符串,我会收到"hello"警告,如果我输入"parker"页面" http://www.google.com "将无法加载.为什么会这样?

编辑:好的,如有人提到我删除" http://www.google.com "并添加" http://stackoverflow.com ",但它没有解决我的问题.我甚至无法加载位于同一目录中的本地html页面

if(filmName == "parker")
                 window.location.assign("index.html"); // also "./index.html" or ./index/html
Run Code Online (Sandbox Code Playgroud)

即便这样也行不通.怎么了

我也有jquery库:那就是Jquery UI和Jquery

这个问题我需要更多的信息.以下是最终的修改

<!DOCTYPE html>
<html lang="en">
  <head>
  <meta charset="utf-8" />
  <!-- Other style and js libraries, including Jquery Ui and regular jquery -->

 <script>
   $(document).ready(function() {
    $(".youtube").fancybox();
   }); // end ready
</script>

<script>    
  $(document).ready(function(e) { 
    $('.tooltip').hide();
    $('.trigger').mouseover(function() {
    /* Rest of it, which is not necessary here */   
</script>

    <script type="text/javascript">
        function myfunc()
        {
            var filmName = document.getElementById("mysearch-text").value;
            alert(filmName); // for debugging
            if(filmName == "parker")
                 window.location.assign("index.html");
            else
                alert("hello");

        }       
    </script>

</head>
<body>
  <div id='mysearch-box'>
    <form  id='mysearch-form'>
      <input id='mysearch-text'  placeholder='' type='text'/><!-- Input here guys -->
      <button class = "none" id='mysearch-button' onclick = "myfunc()">Search</button>
     <!-- press this button after entering "parker" -->
    </form>
  </div>

<!-- Rest of the HTML page -->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Moh*_*Ted 8

我假设您正在使用表单onsubmit事件来调用myfunc,如果是这样,您必须通过return false;(例如)阻止默认行为

HTML:

<form id="form">
    <input type="text" id="mysearch-text">
    <input type="submit" value="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)

JS:

<script>
    window.onload = function () {
        document.getElementById("form").onsubmit = function () {
            var filmName = document.getElementById("mysearch-text").value;
            alert(filmName);
            if (filmName == "parker") window.location.href = "http://www.w3fools.com";
            else alert("hello");
            return false; //prevent the default behavior of the submit event
        }
    }        
</script> 
Run Code Online (Sandbox Code Playgroud)

  • OHHH !!!!! 哦,CAPTAIN,MY CAPTAIN.有用.非常感谢.你怎么猜的?经验?.但是你非常感谢我.:) (2认同)