gpt4 book ai didi

javascript - Laravel-如果我将变量传递给 Controller ​​并保存在数据库中,如何在 javascript 中使用 switch case

转载 作者:行者123 更新时间:2023-11-29 21:00:19 24 4
gpt4 key购买 nike

我在 laravel 工作,我发送了很多数据,包括下拉列表,它们有值,0,1,2,这意味着顶部,右侧,左侧问题是当我仅在 Controller 中使用更新功能时,我得到 0 或 1 或 2取决于我输入的内容,必须刷新才能看到文本,因为我在单击保存后使用 json 和 ajax。

HTML

<select class="form-control" id="position_edit" name="position_edit">
<option value="0">TOP</option>
<option value="1">RIGHT</option>
<option value="2">LEFT</option>
<option value="3">BOTTOM</option>
</select>

控制者

public function updatebanner(Request $request)
{
if ($request->hasFile('image')) {
$imagename = time().'.'.$request->file('image')->getClientOriginalExtension();
$path =$request->file('image')->storeAs('/images', $imagename);

$post = Advertisement::findOrFail($request->id);
$post->adver_title = $request->title_edit;
$post->adver_url = $request->url_edit;
$post->adver_position = $request->position_edit;
$post->adver_photo = $imagename;
$post->save();
}

ajax 代码

$('.item' + data.id).replaceWith("
<tr class='item" + data.id + "'>
<td class='col1'>" + data.id + "</td>
<td><img src='images/" + data.adver_photo + "'style='width: 50%;margin-left: 35px;border-radius: 3px;border: 1px solid #1a2732;' /> </td>
<td>" + data.adver_title + "</td>
<td>" +data.adver_position+ "</td>
</td>
<td class='text-center'><input type='checkbox' class='edit_published' data-id='" + data.id + " '></td>
<td style='text-align:center;'>Right Now!</td>
<td><button class='show-modal btn btn-success' data-id='" + data.id + "' data-titlefield='" + data.adver_title + "' data-photofield='images/" + data.adver_photo + "'><span class='fa fa-eye'></span> Show</button> <button class='edit-modal btn btn-info' data-id='" + data.id + "' data-titlefield='" + data.adver_title + "' data-photofield='images/" + data.adver_photo + "'><span class='fa fa-pencil-square'></span> Edit</button> <button class='delete-modal btn btn-danger' data-id='" + data.id + "' data-titlefield='" + data.adver_title + "' data-photofield='images/" + data.adver_photo + "'><span class='fa fa-trash-o'></span> Delete</button></td>
</tr>
");

我尝试使用下一个代码,但如果 adver_position 值保存在数据库中,问题就不起作用,它将返回空白,没有任何数字或文本,必须刷新页面才能看到文本

   var content = "<tr class='item" + data.id + "'> "
+ "<td class='col1'>" + data.id + "</td> "
+ "<td><img src='images/" + data.adver_photo + "'style='width: 50%;margin-left: 35px;border-radius: 3px;border: 1px solid #1a2732;' /> </td>"
+ "<td>" + data.adver_title + "</td>"
+ "<td> ";

switch(data.adver_position) {
case 0:
content = content + "TOP";
break;
case 1:
content = content + "RIGHT";
break;
case 2:
content = content + "LEFT";
break;
case 3:
content = content + "BOTTOM";
break;
}

content = content + "</td> </tr>"


$('.item' + data.id).replaceWith(content);

最佳答案

根据我对你的问题的理解,我认为当你从你的 Controller 保存到数据库时,你不会向前端返回响应,这可以用来更新你的内容。

为什么不做这样的事情呢?

public function updatebanner(Request $request)
{
if ($request->hasFile('image')) {
$imagename = time().'.'.$request->file('image')->getClientOriginalExtension();
$path =$request->file('image')->storeAs('/images', $imagename);

$post = Advertisement::findOrFail($request->id);
$post->adver_title = $request->title_edit;
$post->adver_url = $request->url_edit;
$post->adver_position = $request->position_edit;
$post->adver_photo = $imagename;
$post->save();
return response(['item'=>$post]); /// return response

}

关于javascript - Laravel-如果我将变量传递给 Controller ​​并保存在数据库中,如何在 javascript 中使用 switch case,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46826351/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com