1

我正在创建一个漂亮的 url 基础网络,但我在获取表单变量时遇到了问题。我有一个类似的网址localhost/events/1(1 是一个获取变量 $_GET['eventID'])。我想使用如下形式发送 eventID

<select name='eventID'>
    <option value='1'>firstEvent</option>
    <option value='2'>secondEvent</option>
    <option value='3'>thirdEvent</option>
</select>

但是当我点击提交按钮将信息发送到页面并且我的网址更改为此

localhost/events/?eventID=1

但我希望我的网址看起来像这样

localhost/events/1

我怎么能做到这一点?

4

3 回答 3

1

According to W3C The question mark is part of the definition of form submission with the GET method, so you can't actually do this. but you can redirect the page with javascript.

 <form id="form" method="get">
 <select name='eventID'>
     <option value='1'>firstEvent</option>
     <option value='2'>secondEvent</option>
     <option value='3'>thirdEvent</option>
 </select>
 <input type="button" onclick="document.location.href= '/events/' + form.eventID.value" value="Submit">
 </form>
于 2014-12-04T17:16:05.123 回答
1

If you want to do this at the client side, you can use the javascript onsubmit event and redirect to the page that you want. For example:

<script type="text/javascript">
function yourFunction()
{
    var sel = document.getElementById('eventID');
    if (sel.selectedIndex !== -1)
    {
        window.location.href = '/events/' + sel.options[sel.selectedIndex].value;
    }

    return false;
}
</script>

<form method="get" onsubmit="return yourFunction()">
<select name="eventID" id="eventID">
    <option value="1">firstEvent</option>
    <option value="2">secondEvent</option>
    <option value="3">thirdEvent</option>
</select>
<input type="submit">
</form>
于 2014-12-04T17:09:36.517 回答
0

谢谢大家。我想要一个没有 javascript 或 jquery 的解决方案,但似乎没有它们两者都没有办法做到这一点。我终于在Jquery中写了一些代码来解决这个问题。

 <form id="events" method="get">
    <select name='eventID'>
     <option value='1'>firstEvent</option>
     <option value='2'>secondEvent</option>
     <option value='3'>thirdEvent</option>
    </select>
 <input type="button" onclick="document.location.href= '/events/' + form.eventID.value" value="Submit">
 </form>

$('#events').on('submit', function(e){
    e.preventDefault();
    document.location.href= '/events/' + $(this).find('select#sportID').val();
})
于 2014-12-04T19:18:08.270 回答