开发者

easy way to show image next to textbox?

开发者 https://www.devze.com 2022-12-10 07:34 出处:网络
I trying to validate a form using ajax...I have got it to show either a cross or tick if the box passes valdation. This is done by showing or hiding a div tag, is there an easier way without me 开发者

I trying to validate a form using ajax...I have got it to show either a cross or tick if the box passes valdation. This is done by showing or hiding a div tag, is there an easier way without me 开发者_StackOverflow中文版having to have a div tag for each cross & tick as this would use about 20 div tags.

Thanks


You could use the CSS :before property to do this:

.tick:before {
    content:url(tick.gif);
}

.cross:before {
    content:url(cross.gif);
}

(you'll probably have to tweak the CSS a bit to get the image to display in the proper position)

Then in your javascript, just add the class tick or cross to each text box that you want to display an image next to.


Since your validation depends on javascript you could leave the divs out of the html initially and insert them after validating a field. Let's say your source is something like:

<p><input id="email" name="email" type="text" value="" /></p>

Your script could be something like:

var emailField = document.getElementById('email');
if( isValid(emailField) ){
var tick = document.createElement('DIV');
tick.className = 'tick';
emailField.parentNode.appendChild(tick);
}else{
var cross= document.createElement('DIV');
cross.className = 'cross';
emailField.parentNode.appendChild(cross);
}

You add the classes '.tick' and '.cross' to your css and apply background images. Of course, you need some checking if the element was already inserted by a previous validation. This is just a simple example.

This way you don't need the extra divs initially.

0

精彩评论

暂无评论...
验证码 换一张
取 消