[英]Show/Hide DIV when Select Box is changed
I have a form where the end user can select various options to create a hardware device listing. 我有一个表格,最终用户可以选择各种选项来创建硬件设备列表。 I would like to hide specific fields (IP Allocations) in a DIV when no value is selected in a Select Box (default), but once they choose a value in the Select Box, I want to show the DIV containing the extra fields.
当在“选择框”(默认)中未选择任何值时,我想在DIV中隐藏特定字段(IP分配),但是一旦他们在“选择框”中选择了一个值,我想显示包含额外字段的DIV。 Can I do this and repopulate the contents of the hidden fields by calling a PHP function from the JS?
我可以这样做,并通过从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>
You'd be better off hiding it by default and then showing it. 您最好将其默认隐藏起来,然后再显示出来。
This is the most basic way of doing it: 这是最基本的方法:
// 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();
}
});
Please supply the class names for a more specific example. 请提供类名以获取更具体的示例。
As for calling a PHP function through JavaScript, you'd need to make an AJAX call for that. 至于通过JavaScript调用PHP函数,则需要为此进行AJAX调用。
Here is a basic example: 这是一个基本示例:
$.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
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.