使用Javascript的交互式HTML表单

php*_*oob 1 html javascript php forms jquery

如何制作交互式HTML表单,例如:

<select name="command">
    <option value="send">Send</option>
    <option value="cancel">Cancel</option>
</select>

<!-- 
    I want the following text input hidden by default,
    but active only if option "Cancel" is selected
-->
<input type="text" name="cancel_reason" id="needreason">
Run Code Online (Sandbox Code Playgroud)

我想默认隐藏"cancel_reason"输入字段,如果之前选择了dropdown-option"Cancel",则会显示,否则它应该保持隐藏状态.

Sae*_*ati 5

$('select[name=command]').change(function(){
    if($(this).val() == 'cancel')
    {
       $('#needreason').show();
    }
    else
    {
       $('#needreason').hide();
    }
});
Run Code Online (Sandbox Code Playgroud)