duancao1951 2013-07-09 14:08
浏览 176
已采纳

一起使用<select>和<input type ='text'>

I'm trying to use both a <select> field and <input type='text'> field together with a $_GET method. Basically I'm hoping to have something like this:

<select>
    <option name='artist'>Artist</option>
    <option name='song'>Song</option>
</select>
<input type='text' name='value' />

And then hopefully for it to redirect as ?artist=value or ?song=value depending on the selected. I hope this makes sense and any help would be appreciated.

  • 写回答

2条回答 默认 最新

  • douyue1481 2013-07-09 14:11
    关注

    No, you cannot do that. Not directly.

    You should attach the name attribute to the select element. Then $_GET will contain your_select_name=option_value.

    Then just correlate your $_GET['type'] and $_GET['value'] in the backend.

    <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
    

    P.S.: If you need to be strict about forming your URL, you can either provide two inputs and rig up a simple JS script that will hide the input not related to your select choice.

    Actually, this idea calls for a little elaboration:

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?