Laravel-JQuery键控仅在Laravel json中的parent_id为1时起作用
在我的Laravel-5.8项目中,我试图在文本框上更改下拉加载值Laravel-JQuery键控仅在Laravel json中的parent_id为1时起作用,jquery,laravel,Jquery,Laravel,在我的Laravel-5.8项目中,我试图在文本框上更改下拉加载值 CREATE TABLE `appraisal_goal_types` ( `id` int(11) NOT NULL, `name` varchar(200) NOT NULL, `parent_id` int(11) DEFAULT NULL, `max_score` int(11) DEFAULT 0, ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 图表: 控制器 public
CREATE TABLE `appraisal_goal_types` (
`id` int(11) NOT NULL,
`name` varchar(200) NOT NULL,
`parent_id` int(11) DEFAULT NULL,
`max_score` int(11) DEFAULT 0,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
图表:
控制器
public function findScore(Request $request)
{
$userCompany = Auth::user()->company_id;
$userEmployee = Auth::user()->employee_id;
$identities = DB::table('appraisal_identity')->select('id')->where('company_id', $userCompany)->where('is_current', 1)->first()->id;
$child = DB::table('appraisal_goal_types')->where('company_id', $userCompany)->where('id',$request->id)->first();
$parentid = DB::table('appraisal_goal_types')->select('parent_id')->where('company_id', $userCompany)->where('id',$request->id)->first()->parent_id;
if(empty($child))
{
abort(404);
}
$weightedscore = 0;
$weightedscore = DB::table('appraisal_goals')->select(DB::raw("SUM(weighted_score) as weighted_score"))->where('appraisal_identity_id', $identities)->where('employee_id', $userEmployee)->where('parent_id', $parentid)->get();
$maxscore = DB::table('appraisal_goal_types')->select('max_score')->find($child->parent_id);
return response()->json([
'maxscore' => $maxscore->max_score,
'weightedscore' => $weightedscore
]);
}
还有routes/web.php
Route::get('get/findScore','Appraisal\AppraisalGoalsController@findScore')->name('get.scores.all');
看法
我的观察结果是,它在parent_id为1的情况下运行良好,但在其他情况下(5和6)运行失败
为什么以及如何解决这个问题?试试这个
<input type="number" name="weighted_score" id="total_weighted_score" placeholder="Enter weighted score here" class="form-control" max="120">
<script>
$("input[name=weighted_score]").on('blur keyup keydown change paste',function(){
var value = $(this).value();
let max_score = $("#max_score").val();
let weighted_score = $("#weighted_score").val();
let sumValue = parseInt(weighted_score) + parseInt(value);
if (sumValue > max_score) {
alert("sum value is greater than max score");
$("#total_weighted_score").val('');
return false;
}
})
</script>
$(“输入[name=weighted_score]”)。在('blur keypup keypdown change paste',function()上{
var value=$(this.value();
设max#u score=$(“#max#u score”).val();
设加权分数=$(“#加权分数”).val();
设sumValue=parseInt(加权分数)+parseInt(值);
如果(sumValue>max_分数){
警报(“总和值大于最大分数”);
$(“#加权总分”).val(“”);
返回false;
}
})
<div class="col-12 col-sm-6">
<div class="form-group">
<label class="control-label"> Goal Type:<span style="color:red;">*</span></label>
<select id="goal_type" class="form-control" name="goal_type_id">
<option value="">Select Goal Type</option>
@foreach ($categories as $category)
<option hidden value="{{ $category->id }}" {{ $category->id == old('category_id') ? 'selected' : '' }}>{{ $category->name }}</option>
@if ($category->children)
@foreach ($category->children as $child)
<option value="{{ $child->id }}" {{ $child->id == old('category_id') ? 'selected' : '' }}> {{ $child->name }}</option>
@endforeach
@endif
@endforeach
</select>
</div>
</div>
<input type="hidden" id="max_score" class="form-control" >
<input type="hidden" id="weighted_score" class="form-control" value="0" >
<div class="col-12 col-sm-6">
<div class="form-group">
<label class="control-label"> Weight(%):<span style="color:red;">*</span></label>
<input type="number" name="weighted_score" id="total_weighted_score" placeholder="Enter weighted score here" class="form-control" max="120" onkeyup="checkScore(this.value)">
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
$(document).on('change', '#goal_type', function() {
var air_id = $(this).val();
var a = $(this).parent();
var op = "";
$.ajax({
type: 'get',
url: '{{ route('get.scores.all') }}',
data: { 'id': air_id },
dataType: 'json', //return data will be json
success: function(data) {
console.log(data.maxscore);
console.log(data.weightedscore);
$('#max_score').val(data.maxscore);
$('#weighted_score').val(data.weightedscore);
},
error:function(){
}
});
});
});
</script>
<script type="text/javascript">
function checkScore(value){
let max_score = $("#max_score").val();
let weighted_score = $("#weighted_score").val();
let sumValue = parseInt(weighted_score) + parseInt(value);
if (sumValue > max_score) {
alert("sum value is greater than max score");
$("#total_weighted_score").val('');
return false;
}
}
</script> to
alert("sum value is greater than max score");
<input type="number" name="weighted_score" id="total_weighted_score" placeholder="Enter weighted score here" class="form-control" max="120">
<script>
$("input[name=weighted_score]").on('blur keyup keydown change paste',function(){
var value = $(this).value();
let max_score = $("#max_score").val();
let weighted_score = $("#weighted_score").val();
let sumValue = parseInt(weighted_score) + parseInt(value);
if (sumValue > max_score) {
alert("sum value is greater than max score");
$("#total_weighted_score").val('');
return false;
}
})
</script>