选择框更改后显示/隐藏DIV

时间:2013-08-27 06:26:30

标签: php javascript jquery ajax html

我有一个表单,最终用户可以选择各种选项来创建硬件设备列表。我想在选择框(默认)中没有选择任何值时隐藏DIV中的特定字段(IP分配),但是一旦他们在选择框中选择了一个值,我想显示包含额外字段的DIV。我可以通过从JS调用PHP函数来重新填充隐藏字段的内容吗?

                <fieldset style="width:30%; float:left;">
                    <label>IP Allocation</label>
                    <select name="ipv4b" style="width:92%;">
                        <?php
                            $ipv4_values = array("id", "ip");
                            display_options_list($ipv4_values, "ipv4", "id", true);
                        ?>
                    </select>
                </fieldset>

enter image description here

1 个答案:

答案 0 :(得分:0)

默认情况下隐藏它然后显示它会更好。

这是最基本的做法:

// I'm just assuming here, I don't know the class, name or etc of
// your select list
$('select[name="datacenter"]').change(function()
{
    // Check if it's empty
    if($(this).val() != '')
    {
        // Again, I'm assuming here since I don't know the name of your class
        $('.ip-allocation-div').show();
    }

    else
    {
        // No value set, hide it.
        $('.ip-allocation-div').show();
    }
});

请提供更具体的示例的类名。

至于通过JavaScript调用PHP函数,您需要对此进行AJAX调用 这是一个基本的例子:

$.ajax(
{
    url: 'path/to/page.php',

    // This should be an object of data you want to send,  
    // remove if no data will be sent
    data: {}, 
    type: 'POST',

    // Your callback function
    success: function(response)
    {
        // Do something with the data
    }
})