0

我有一个表单,最终用户可以在其中选择各种选项来创建硬件设备列表。当在选择框中未选择值时,我想将特定字段(IP分配)隐藏在DIV中(默认值),但是一旦他们在选择框中选择一个值,我想显示包含多余字段的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>

在此处输入图像描述

4

1 回答 1

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
    }
})
于 2013-08-27T06:48:38.123 回答