3

这个问题对我来说似乎很奇怪,我似乎无法解决它。我有一个简单的 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 时,它可以工作。请注意,在 <form...> 标记中,我没有将action属性放入其中。我不想打开新浏览器,只需重定向到同一浏览器中的新网址。

有没有办法重定向它?

4

4 回答 4

1

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

于 2012-06-07T17:42:05.250 回答
1

好的,只是一个想法。由于您尚未设置action参数,因此按钮的默认行为submit是重新加载同一页面。你通过处理它来改变那个行为onclick。也许有一个冲突可以通过return false;在单击处理程序的末尾来解决,这会阻止该事件的默认操作。

于 2012-06-07T17:43:18.067 回答
0

这里

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

    //capture all values from the form
    var allWords = which.txtAll.value;

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


<form id="form1" method="get" onsubmit="this.action=myJs(this)">
<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" />
            </td>
        </tr>
    </table>
</div>
</form>

详细说明评论:

<script>
window.onload=function() {
  document.getElementById("Submit").onclick=function() {
     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.....
    location = CONSTANT_SITE_TARGET + allWords + morestring;
  }
}
</script>
<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="button" id="Submit" value="Submit" />
            </td>
        </tr>
    </table>
</div>
于 2012-06-07T17:45:04.837 回答
-1

尝试以下重定向选项之一。我注释掉了其中的三个以使代码保持有效,但您可以尝试使用其中的任何一个,看看它是否适合您的需要。

    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.location.href= url;
     //window.navigate(url);
     //self.location(url);
     //top.location(url);
于 2012-06-07T17:52:23.690 回答