Javascript中的重定向表单传递值
我想创建一个表单,根据3个值将用户重定向到另一个页面: 1)Javascript中的重定向表单传递值,javascript,forms,redirect,Javascript,Forms,Redirect,我想创建一个表单,根据3个值将用户重定向到另一个页面: 1) 选择firstcurrency 2) 选择secondcurrency 3) 输入金额 提交表单时,必须将用户重定向到以下地址: firstcurrency-secondcurrency/?价格=金额 表格如下: <form id="currency"> <input id="amount" name="amount" class="amount" type="text" value="" /> <s
选择
firstcurrency
2) 选择
secondcurrency
3) 输入
金额
提交表单时,必须将用户重定向到以下地址:
firstcurrency
-secondcurrency
/?价格=金额
表格如下:
<form id="currency">
<input id="amount" name="amount" class="amount" type="text" value="" />
<select id="firstcurrency" class="selex" name="firstcurrency">
<option value="USD">USD</option>
<option value="EUR">EUR</option>
</select>
<select id="secondcurrency" class="selex" name="secondcurrency">
<option value="AUD">AUD</option>
<option value="JPY">JPY</option>
</select>
<input class="button" type="submit" value="Redirect Me"/>
</form>
美元
欧元
澳元
日元
您将使用什么Javascript代码?尝试此操作。使用表单
名称和值的内部元素创建对象。然后使用url的受尊重位置调用对象属性,并使用window.location.href
进行分配
注意*:为了便于查看,我添加了var-url
。您需要使用window.location.href=url
//http://www.example.com/sample/firstcurrency-secondcurrency/?price=amount
函数重定向(that){
var params={};
对于(var i=0;i请尝试此操作。使用表单name and value
的内部元素创建对象。然后使用url的受尊重位置调用对象属性,并使用window.location.href
进行分配
注意*:为了便于查看,我添加了var-url
。您需要使用window.location.href=url
//http://www.example.com/sample/firstcurrency-secondcurrency/?price=amount
函数重定向(that){
var params={};
对于(var i=0;i
在“提交”按钮上:
<input class="button" type="submit" value="Redirect Me" onclick="Redirect()"/>
在“提交”按钮上:
<input class="button" type="submit" value="Redirect Me" onclick="Redirect()"/>
我会这样做:
var form = document.getElementById('currency');
form.onsubmit = function(e){
var firstcurrency = document.getElementById('firstcurrency').value;
var secondcurrency = document.getElementById('secondcurrency').value;
var price = document.getElementById('amount').value;
e.target.action = "http://www.example.com/sample/"+firstcurrency+"-"+secondcurrency+"/?price="+price;
}
此外,为了实现完全相同的url方案,您必须删除所有输入元素的名称属性,以免它们作为查询参数附加在url的末尾
以下是更新后的表格:
<form id="currency" >
<input id="amount" class="amount" type="text" value="" />
<select id="firstcurrency" class="selex" >
<option value="USD">USD</option>
<option value="EUR">EUR</option>
</select>
<select id="secondcurrency" class="selex" >
<option value="AUD">AUD</option>
<option value="JPY">JPY</option>
</select>
<input class="button" type="submit" value="Redirect Me"/>
</form>
美元
欧元
澳元
日元
我会这样做:
var form = document.getElementById('currency');
form.onsubmit = function(e){
var firstcurrency = document.getElementById('firstcurrency').value;
var secondcurrency = document.getElementById('secondcurrency').value;
var price = document.getElementById('amount').value;
e.target.action = "http://www.example.com/sample/"+firstcurrency+"-"+secondcurrency+"/?price="+price;
}
此外,为了实现完全相同的url方案,您必须删除所有输入元素的名称属性,以免它们作为查询参数附加在url的末尾
以下是更新后的表格:
<form id="currency" >
<input id="amount" class="amount" type="text" value="" />
<select id="firstcurrency" class="selex" >
<option value="USD">USD</option>
<option value="EUR">EUR</option>
</select>
<select id="secondcurrency" class="selex" >
<option value="AUD">AUD</option>
<option value="JPY">JPY</option>
</select>
<input class="button" type="submit" value="Redirect Me"/>
</form>
美元
欧元
澳元
日元
我做了一些测试,但没有得到您的修改。请发布最终版本好吗?@Monzio查看我编辑过的答案。我被添加了最终版本我在本地主机上,但我仍然被重定向到#currenturl#?amount=1&firstcurrency=USD&secondcurrency=AUD我已经包含了jquery 3.2.1。有什么想法吗?@Monzio是的,我知道。试试最新更新的一个。当然可以。我做了一些测试,但没有得到你的修改。你能发布最终版本吗?@Monzio查看我编辑过的答案。我被添加了最终版本。我在本地主机上,但我仍然被重定向到#currenturl#?amount=1&firstcurrency=USD&secondcurrency=AUD。我已经包括了jquery 3.2.1。有什么想法吗?@Monzio是的我知道。试试最新更新的。肯定有用。我被测试被重定向到#CURRENTURL#/?amount=4&firstcurrency=USD&secondcurrency=AUDtrywindow.location.href
而不是window.location.replace
我被重定向到#CURRENTURL#/?amount=4&firstcurrency=USD&secondcurrency=AUDtrywindow.location.href
in读取window.location.replace