Javascript替换HTML字符

Javascript Replace HTML Characters

本文关键字:字符 HTML 替换 Javascript      更新时间:2023-09-26

我完全不懂Javascript。我想要的是有一个搜索表单,表单的输入应该更改url,然后它会用新的url打开一个新的选项卡。

例如,当我输入"production"时,url将变为http://production-test.com/test/,则会打开一个带有新url的新选项卡。

我知道这应该在Javascript中,但我不知道它将如何返回新的url。

到目前为止,我有这个html根本不起作用。

 <script language="javascript">
 function change_url(result) {
     #var result = document.getElementById().value;
     var new_url = "http://" + result + "-test.com/test/';
     # open new window with the new url
 }
 <form id="search-box" name="search_box" method="get" 
action="http://variable-test.com/test/" target="_blank"">
 <input id="search-text" type="text" autocomplete="off" 
class="form-text-box no-focus" name="c" value="" aria-haspopup="true">
 <input type="radio" id="rel" name="sort" value="1" onchange="saveType('rel');" /><span data-message="TypeOp1">Relevance</span><br />
 <input id="search-button" type="submit" />

假设您的最终目标不仅是在新的选项卡/窗口中打开新的url,而且您还想将表单发送到新选项卡/窗口的新url:

您可以在提交表格之前更改表格的操作:

function change_url(form) { 
     var result = document.getElementById('inputId').value;        
     form.action = "http://" + result + "-test.com/test/";
     return true;
 }

并将其添加到表单标签:

onsubmit="return change_url(this)"

表单现在将被发送到新的url和新的窗口/选项卡(因为您将目标设置为"_blank"

您可以使用window.open

window.open(new_url);

如果您希望在新选项卡/窗口中打开该窗口,请输入'_blank'作为第二个参数

window.open(new_url, '_blank');