早上好。我是一个自我学习的人,正在做家庭项目。我卡住了,需要帮助如何改变从下拉菜单中选择的每个值与ajax成功数据的文本值。
文本代码
<div class="card-title mb-0">
<h5 class="card-title m-0 me-2">Milk Sell Statistics</h5>
<small id="filter" class="text-muted">For : {{$yesterday}}</small>
</div>
<div class="dropdown">
<button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button"
id="growthReportId" data-bs-toggle="dropdown" aria-haspopup="true" aria-
expanded="false"> Yesterday
</button>
<div id="milkstatementdropdown" name="milkstatementdropdown"class="dropdown-menu dropdown-
menu- end" aria-labelledby="milkstatementdropdown">
<li><a class="dropdown-item" href="#Yesterday" id={{$yesterdaydate}} data-
order="a">Yesterday</a></li>
<li><a class="dropdown-item" href="#Last7Days" id={{$last7daysdate}}>Last 7 Days</a>
</li>
<li><a class="dropdown-item" href="#currentmonth" id={{$currentmonthstartdate}}>Current
Month</a></li>
</div>
</div>
这里需要更改"{{$昨天}}"根据ajax数据持续时间,如"For 7 days/For 15 days ">
ajax代码
$(function($) {
$('.dropdown li a').click(function() {
var element = $(this);
var selectedperiod = element.attr("id");
//alert (selectedperiod)
$.ajaxSetup({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')} });
$.ajax({
type: 'post',
url: '/milksalereportchangevalues',
data: {
inputdate : selectedperiod,
},
success: function(data) {
alert (document.getElementById(["filter"]))
$('#filter label').text('my text');
}
})
});
});
网络预览中显示的数据为
["2022-12-19", "2022-12-25", "215.00", "168.00", "91.50", "474.50"]
0:"2022 - 12 - 19";1:"2022 - 12 - 25 -";2:"215.00";3.:"168.00";4:"91.50";5:"474.50">
提前感谢您的帮助
建议:
从服务器发送响应时,将其作为键值
发送{"2022-12-19" : "somevalue", "2022-12-25": "some value"}
读取和呈现
的代码片段const today = new Date() ; ;
const yesterday = new Date(today)
yesterday.setDate(yesterday.getDate() - 1)
yesterday.toDateString();
let yestStr = yesterday.getFullYear() +'-' + (yesterday.getMonth() + 1) + '-' + yesterday.getDate() ;
console.log(yestStr);
$('#filter').text(data[yestStr]);