我正在通过Laravel发送邮件,并在发送邮件后重新加载网页。所以,我想重新加载后重点关注FORM元素。是否有可能在特定时刻获取javascript?
Main.blade.php
@if(Session::has('flash_message'))
<script>
$(document).ready(function(){
alert('abcd');
$('[name=sentMessage]').focus();
});
</script>
<div class="alert alert-success">{{ Session::get('flash_message')}}</div>
@endif
<form name="sentMessage" method="post" action="{{ route('demo.store') }}">
{{ csrf_field() }}
Controller.php这样
public function store(Request $request){
$this->validate($request, [
'name' => 'required',
'email' => 'required|email',
'phone' => 'required',
'message' => 'required'
]);
Mail::send('emails.contact-message',[
'name' => $request->name,
'email' => $request->email,
'phone' => $request->phone,
'msg' => $request->message
], function($mail) use ($request){
$mail->from($request->email, $request->name);
$mail->to('dlgocjs1@gmail.com')->subject('A Message From HCL Portfolio Website');
});
return redirect()->back()->with('flash_message', 'Thank you for your interest');
}
答案 0 :(得分:0)
getElementByName()
不存在,只有getElementsByName()
。
由于您使用的是jQuery,因此可以执行以下操作:
$('[name=sentMessage]').focus();
或使用autofocus
属性:
<... name="sentMessage" @if(Session::has('flash_message'))autofocus@endif ...>