![](/img/trans.png)
[英]Laravel- How to "catch" a variable with Javascript passed to a blade.php view
[英]Laravel- How to use switch case in javascript if I passed the variable to controller and saved in database
我在 laravel 工作,我發送了許多數據,包括具有值的下拉列表,0,1,2,這意味着頂部,右側,左側問題是當我只在控制器中使用更新功能時,我得到 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);
根據我對您的問題的理解,我認為當您從控制器保存到數據庫時,您不會向前端返回響應,該響應可用於更新您的內容。
為什么不做這樣的事情?
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
}
你可以試試:
content += $('#position_edit option:checked').text();
而不是switch
。 這將簡單地分配所選位置的文本。
編輯:替換所有這些:
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;
}
使用提供的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.