我在这里查看了无数示例,但不确定我在做什么错。我正在尝试根据下拉菜单中选择的值更改表单操作。
基本上,HTML看起来像这样:
<form id="storetable" autocomplete="off" action="" method="post">
<label>STORE:</label>
<input type="text" list="storeID" name="store" placeholder="Choose a store..." required>
<datalist id="storeID">
<option value="cwb_coins">Causeway Bay</option>
<option value="wc_coins">Wan Chai</option>
<option value="lck_coins">Lai Chi Kok</option>
<option value="tp_coins">Tai Po</option>
</datalist>
</form
javascript:
<script>
$(document).ready(function(){
$("#storeID").change(function(){
var url = $(this).children(":selected").text(); //get the selected option value
switch (url)
{
case "cwb_coins":
$("#storetable").attr('action','cwb_coin_code.php');
//changing action attribute of form to cwb_coin_code.php
break;
case "wc_coins":
$("#storetable").attr('action','wc_coin_code.php');
break;
case "lck_coins":
$("#storetable").attr('action','lck_coin_code.php');
break;
case "tp_coins":
$("#storetable").attr('action','tp_coin_code.php');
break;
default:
$("#storetable").attr('action', '#');
}
});
});
</script>
答案 0 :(得分:0)
应该像更改时那样获取值,并将action属性设置为'cwb_coin_code.php'+ value;
$("#storeID").change(function() {
$("#storetable").attr("action", "cwb_coin_code.php" + $(this).val());
});