[英]Change the form action when an onchange event is triggered on <select> with 2 forms on page
[英]PHP: change page action of a form by url in select
嗨,我的表格有問題。 我知道問題很簡單,但我無法解決。 好吧,這是我的表格:
<form id="search" method="post" action="cerca_redirect2.php" >
<select id="tipo" name="tipo"class="chzn-select" style="width:165px;" tabindex="1" >
<option value="http://case.vortigo.it/vendita-immobili/index.php"> Vendita</option>
<option value="http://case.vortigo.it/affitto-immobili/index.php">Affitto</option>
</select>
<input id="field" name="field" type="text" value=""/>
<input id="submit" type="submit" value="" />
</form>
我的目標是當我選擇“ Vendita”並提交表格時,對於每個選擇,我都必須進入選擇“ Vendita”中的網址。 有人可以幫助我嗎? 謝謝
在服務器端php代碼中,執行以下操作
if (isset($_POST['tipo']) && !empty($_POST['tipo']))
{
header('Location: ' . $_POST['tipo']);
}
注意:這是一個非常基本的版本,您將希望通過維護服務器上的URL列表或類似內容來確保URL有效。
有很多不同的方法,但是您可以例如使用以下方式:請參閱表單定義中的onsubmit部分
<form id="search" method="post" action="cerca_redirect2.php" onsubmit="this.action=document.getElementById('tipo')[document.getElementById('tipo').selectedIndex].value" >
您要基於選擇更改表單的操作嗎? 以下應符合您的要求。
$('#tipo').on('change', function() {
var newAction = this.val();
$('#search').prop('action', newAction);
}
更新
您將需要將此代碼包裝在$(document).ready()
以便在加載DOM之后注冊該事件。
像這樣:
$(document).ready(function() {
$('#tipo').on('change', function() {
var newAction = this.val();
$('#search').prop('action', newAction);
}
});
如果我正確理解了您的問題,則需要一種方法來將操作值更改為所選選項的值
這是怎么做的
$(document).ready(function(){
$("#tipo").on("change", function(){
$("#search").attr("action", $(this).val());
});
});
演示: http : //jsfiddle.net/6ybMP/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.