实现的效果图
<?php class Car{ private $carColor; private $carType; public function __construct($carColor,$carType){ $this->carColor=$carColor; $this->carType=$carType; } public function getColor(){ switch ($this->carColor) { case '0': $carColor="白色"; break; case '1': $carColor='黑色'; break; case '2': $carColor='蓝色'; break; default: $carColor='没有获得此车的颜色'; break; } return $carColor; } public function getType(){ switch ($this->carType) { case '0': $carType='宝马'; break; case '1': $carType='奔驰'; break; case '2': $carType='大众'; break; default: $carType='没有获得此车的品牌'; break; } return $carType; } public function getInfo(){ return $this->getColor().$this->getType(); } } -------------------------------封装的类 if(isset($_POST['a'])&&isset($_POST['b'])){ $carColor=$_POST['a']; $carType=$_POST['b']; $car=new Car($carColor,$carType); echo $car->getInfo(); }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <script type="text/javascript" src="jquery.js"></script> <title>测试一下谢谢</title> </head> <body> <style type="text/css"> .div1{ color: red; font-size: 20px; } </style> <center> 请选择您的车的颜色:<br> <select name="color" id="color" size="1"> <option value="0" selected="selected">白色</option> <option value="1">黑色</option> <option value="2">蓝色</option> </select> <br> 请选择车的型号:<br> <select name="type" id="type" size="1"> <option value="0" selected="selected">宝马</option> <option value="1">奔驰</option> <option value="2">大众</option> </select> <button class="btn" id="btn">提交</button><br> 我的车辆信息:<div class="div1"></div> </center> </body> <script type="text/javascript"> $("#btn").click(function(){ var a=$('#color').val(); var b=$('#type').val(); $.post('server.php',{"a":a,"b":b},function(data){ $(".div1").html(data); }); }); </script> </html>