javascript - window.location.assign ("link"), 不工作

标签 javascript html 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>

如果我输入任何其他字符串,我会收到“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

即使这样也行不通。怎么了

我也有 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>

最佳答案

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

HTML:

<form id="form">
    <input type="text" id="mysearch-text">
    <input type="submit" value="Submit">
</form>

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> 

关于javascript - window.location.assign ("link"), 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16398690/

相关文章:

javascript - 添加动画以使用 Tab 键更改焦点

html - 将文本放在行 div 的前面

php - Laravel 没有将样式表添加到头部,而是添加到主体

javascript - 一个盒子不适合不同的屏幕分辨率

javascript - 为什么这个倒计时 javascript 不能在 chrome 以外的任何浏览器上运行

javascript - ChartJS 未更新

javascript - 在 Laravel Blade 模板中使用 JS/JQuery 时搜索栏过滤器的问题

html - 如何将自定义 css 样式应用于来自后端代码的表格

javascript - IE 的自定义 css 属性

javascript - 如何将内联 JS 编码转换为外部样式表?