简体   繁体   中英

onfocus/onblur in laravel form

There is html form code, writing that into laravel which converts this.value=='' into this.value==#039; how to write this.value=='' in laravel ?

<input class="form-control select-design" required value="Years of Work Experience" name="experience" type="text" required onblur="if(this.value==''){ this.value='Years of Work Experience'; this.style.color='#BBB';}" onfocus="if(this.value=='Years of Work Experience'){ this.value=''; this.style.color='#000';}" style="color:#BBB;">

Writing it into laravel(php)

{!! Form::text('experience', null, ['class' => 'form-control select-design','value' => 'Years of Work Experience','name'=>"experience",'type'=>"text",'onblur'=>"if(this.value==''){ this.value='Years of Work Experience'; this.style.color='#BBB';}",'onfocus' => "if(this.value=='Years of Work Experience'){ this.value=''; this.style.color='#000';}",'style'=>"color:#BBB;"]) !!}

It shows actual code like this

<input class="form-control select-design" name="experience" type="text" onblur="if(this.value==&#039;&#039;){ this.value=&#039;Years of Work Experience&#039;; this.style.color=&#039;#BBB&#039;;}" onfocus="if(this.value==&#039;Years of Work Experience&#039;){ this.value=&#039;&#039;; this.style.color=&#039;#000&#039;;}" style="color:#BBB;">

Is there any issue with syntax ?

You can't do this in Form::text() since it's using htmlspecialchars() function and you can't change that. What you can do is create custom macro . But still I believe it's terrible idea to hardcode JS functions into Laravel Collective forms.

Best thing you can do is to move all JS logic to JS file and do something like this:

$("#element").blur(....);

Or you could use simple HTML forms. But moving all JS logic out from HTML is the best choice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM