[英]Using <select> and <input type='text'> together
我正在尝试同时使用<select>
字段和<input type='text'>
字段以及$ _GET方法。 基本上我希望有这样的东西:
<select>
<option name='artist'>Artist</option>
<option name='song'>Song</option>
</select>
<input type='text' name='value' />
然后希望它根据所选择的重定向为?artist=value
或?song=value
”。 我希望这是有道理的,任何帮助将不胜感激。
不,你做不到。 不是直接的。
您应该将name
属性附加到select
元素。 然后$_GET
将包含your_select_name=option_value
。
然后只关联后端的$_GET['type']
和$_GET['value']
。
<form method="GET">
<select name="type">
<option value='artist'>Artist</option>
<option value='song'>Song</option>
</select>
<input type='text' name='value' />
</form>
<?php
echo $_GET['type']; // 'artist' or 'song'
echo $_GET['value']; // value of text input
PS:如果你需要严格要求形成你的URL,你可以提供两个输入并装配一个简单的JS脚本,它将隐藏与你的选择选项无关的输入。
实际上,这个想法需要一点点阐述:
<form method="GET">
<select id='type'>
<option name='artist'>Artist</option>
<option selected name='song'>Song</option>
</select>
<input class='inp' id='song' type='text' name='song' />
<input class='inp' style='display:none' id='artist' type='text' name='artist' />
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
$('#type').change(function() {
$('.inp').val('').hide().prop('disabled', true); // hide and clear both inputs
$('#'+$('#type').val() ).prop('disabled', false).show(); //show input corresponding to your select
// Note the prop('disabled') calls - you want to disable the unused input so it does not add an empty key to your query string.
}
</script>
我认为你应该将name属性放在<select>
标签上。
<select name="Category">
<option>Song</option>
<option>Artist</option>
</select>
<input type="text" name="Value"/>
那么验证已选择的例子(PHP)应该相当容易:
if($_GET['Category'] == "Song")
{
//Do stuff here with $_GET['Value']
}
else if($_GET['Category'] == "Artist")
{
//Do stuuf here with $_GET['Value']
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.