HTML
<body>
<div class="header">
<input type="text" name="number"><button>随机产生</button>
</div>
</body>
css
.header{
width:80%;
height:80px;
min-width: 505px;
margin:0 auto;
text-align: center;
}
.header input{
height: 60px;
width: 400px;
border: 1px solid #ccc;
box-sizing: border-box;
vertical-align: middle;
outline: none;
text-indent: 30px;
font-size: 20px;
}
.header button{
background-color: gold;
width: 100px;
height: 60px;
line-height: 60px;
color: #FFF;
border: 1px solid #ccc;
vertical-align: middle;
outline: none;
}
.list{
width: 80%;
min-height: 505px;
background-color: beige;
margin:0 auto;
}
.list span{
display: inline-block;
background-color: aquamarine;
color: #333;
padding:10px 20px;
border-radius:5px;
margin: 20px;
cursor: pointer;
}
JS
$(function(){
$('button').click(function(){
$('.list').on('click','span',function(){
$(this).css({"backgroundColor":"red",
"cursor":"default",
});
})
$.get('diancaiqi.php',{num:$('input').val()},function(e){
$('.list').html('');
for(var i=0;i<e.length;i++){
$('<span>'+e[i]+'</span>').appendTo($('.list'));
}
},'json')
});
})
PHP
最少一道
<?php
$num = (int)$_GET['num'];
//至少产生一道菜
if($num<=0){
$re= [
'code' => 2000,
'message'=>'至少产生一道菜'
];
echo json_encode($re);
die;
}
随机从菜单获取
$stu_arr = explode('|',file_get_contents('caiming.txt'));
//in_array 判断是否在一个数组中
//mt_rand 产生随机数
$arr = []; //用户保存菜单的编号
for($i=0;$i<$num;$i++){ //用于循环产生菜单编号
$stu_num = mt_rand(0,100); //随机产生
while(in_array($stu_num,$arr)){ //判断是否产生了相同的菜单编号
$stu_num = mt_rand(0,99);;
}
array_push($arr,$stu_num);
}
菜单
caiming.txt
$re = []; //返回的菜单数组
foreach($arr as $k){
array_push($re,$stu_arr[$k]);
file_put_contents('quedingcai.txt',$stu_arr[$k],FILE_APPEND);
}
echo json_encode($re);