根据不同的选择选项传递值

时间:2015-06-20 19:04:40

标签: javascript forms select

我正在尝试根据用户从下拉列表中选择的选项获取不同的值。 例如。当用户从dropdown =>中选择选项1时将值111传递给。$形式的数字

我该怎么做?我应该在//某些代码区域中包含哪些代码?

switch($_POST['outlet']){
    case 'a' : 
        //some code;
        //get value 111 from here
        break;
    case 'b':
        //some code;
        //get value 222 from here
        break;
    case 'c':
        //some code;
        //get value 333 from here
        break;
    default:
        //some code if the post doesn't match anything
}

2 个答案:

答案 0 :(得分:1)

您可以这样做:

<select id="sc" name="outlet">
        <option value="111">1</option>
        <option value="222">2</option>
    </select>

    <select id="sh" name="outlet">
        <option value="333">3</option>
        <option value="444">4</option>
    </select>

    <select id="sw" name="outlet">
        <option value="555">5</option>
        <option value="666">6</option>
    </select>

    Then, just take the value of $_POST['outlet']

答案 1 :(得分:0)

$numbers = null;
switch($_POST['outlet']){
    case 'a' : 
        //some code;
        //get value 111 from here
        $numbers = 111;
        break;
    case 'b':
        //some code;
        //get value 222 from here
        $numbers = 222;
        break;
    case 'c':
        //some code;
        //get value 333 from here
        $numbers = 333;
        break;
    default:
        //some code if the post doesn't match anything
        break;
}

echo is_null($numbers) ? 'Numbers is null' : $numbers; // <- correct value