英文:
Pass value from controller to the blade and dynamically add a number to it
问题
在使用jQuery添加新行时,我想在id中添加一个数字,该数字是从控制器传递到Blade的。
以下是Blade中的JavaScript代码:
$('#add4').click(function(){
var rowCount = $('#table_field4 .trr').length + 1;
var html = '<tr><td><input type="text" value="{{$serialNumberUnits+rowCount}}"></td></tr>';
$('#table_field5').append(html);
});
这是从控制器传递到Blade的变量:
$serialNumberUnits = DB::table('product_unit_prices')->latest('id')->first()->id + 1;
return view('admin.addProduct', compact('serialNumberUnits'));
请注意,上述代码中的变量$serialNumberUnits
会在Blade模板中的JavaScript代码中使用,以生成新行的id。
英文:
I want to add one number in the case of adding a new line by jQuery, and this number is added to the id that I pass from the controller to the blade
Here is the js Code in the blade
$('#add4').click(function(){
var rowCount = $('#table_field4 .trr').length + 1;
var html = '<tr>'+'<td><input type="text" value="{{$serialNumberUnits+'rowCount'}}"></td></tr>';
$('#table_field5').append(html);
});
And here is the variable that I passed from the controller to the blade
$serialNumberUnits = DB::table('product_unit_prices')->latest('id')->first()->id + 1;
return view('admin.addProduct',compact('serialNumberUnits'));
答案1
得分: 2
The $serialNumberUnits will be a string, try to convert to an integer using parseInt() and do the calculations
$('#add4').click(function () {
let rowCount = $('#table_field4 .trr').length + 1;
/**
* Keeping the passed value in a variable
* to keeping it clean
*/
const serialNoUnits = "{{$serialNumberUnits ?? 0}}";
/**
* Parsing into integer and adding
*/
let newValue = parseInt(serialNoUnits) + rowCount;
/**
* Adding value into field
*/
let html = '<tr>' + '<td><input type="text" value="' + newValue + '"></td></tr>';
$('#table_field5').append(html);
});
英文:
The $serialNumberUnits will be a string, try to convert to an integer using parseInt() and do the calculations
$('#add4').click(function () {
let rowCount = $('#table_field4 .trr').length + 1;
/**
* Keeping the passed value in a variable
* to keeping it clean
*/
const serialNoUnits = "{{$serialNumberUnits ?? 0}}";
/**
* Parsing into integer and adding
*/
let newValue = parseInt(serialNoUnits) + rowCount;
/**
* Adding value into field
*/
let html = '<tr>' + '<td><input type="text" value="'+newField+'"></td></tr>';
$('#table_field5').append(html);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论