javascript窗口重定向不会启动

标签 javascript webforms

这个问题对我来说似乎很奇怪,我似乎无法解决它。我有一个简单的 HTML 表单;在里面,我确实有一个文本框和一个按钮,如下所示:

<form id="form1" method="get"> <!-- Note: No Action property -->
    <div>
       <h1>
           Simple Test Form</h1>
       <table border="0" width="400">
           <tr>
               <td align="right">
                   All of these words
               </td>
               <td>
                   <input name="txtAll" id="txtAll" type="text" value="testing keyword" />
               </td>
           </tr>
           <tr>
               <td colspan="2" align="center">
                   <input type="submit" name="Submit" value="Submit" onclick="myJS(this);" />
               </td>
           </tr>
       </table>
   </div>
</form>

MyJS 文件如下:

<script type="text/javascript">
    function myJS(which) {
        var CONSTANT_SITE_TARGET = "http://servername/redirect/target.aspx?text=";

        //capture all values from the form
        var allWords = document.getElementById("txtAll").value;

        var morestring = ""; //More data manipulation here.....
        var url = CONSTANT_SITE_TARGET + allWords + morestring;

        window.open(url);
        //window.location = url;          //Doesn't work
        //window.location.href = url;     //Doesn't work
        //self.location = url;            //Doesn't work
        //top.location = url;             //Doesn't work
    }
</script>

如您所见,它不会重定向到 javascript 中指定的 URL。当我使用 window.open 然后它工作。请注意,在 标记中,我没有将 action 属性放入其中。我不想打开一个新的浏览器,只是重定向到同一个浏览器中的新 url。

有没有办法重定向它?

最佳答案

不要使用表单标签。或者,将按钮的“类型”属性设置为“按钮”,而不是“提交”。单击按钮时表单会提交,但您不希望这种情况发生。删除表单或更改按钮应该修复不正确的重定向。当您不指定操作时,我很确定默认值为当前 URL。

关于javascript窗口重定向不会启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10936912/

相关文章:

javascript - DOMError localforage indexeddb 案例?

javascript - 这是内存泄漏吗? Node.js Handlebars 部分

asp.net-mvc - asp net mvc出现500错误,页面呈现unicode

c# - asp.net 而第一个请求处理允许点击第二个请求

asp.net - 在没有特定需要的情况下在 ASP.Net 页面上使用 Async ="true"可能出现的问题

javascript - 如何刷新页面几秒钟直到下载完成

Javascript - 如果 cookie 存在,则有条件地将复选框添加到 php 页面

javascript - 如何循环遍历2个对象数组并通过匹配id进行合并

javascript - 如果数据未经过验证,则使用自定义验证器更改标签颜色

data-annotations - 在 ASP.NET Webforms 中使用 DataAnnotations 验证