输入时更改图像

编程入门 行业动态 更新时间:2024-10-21 18:46:25
本文介绍了输入时更改图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带JavaScript的小型基本html代码。我想要的只是当我输入3时,它应该给我指定一个图像,并相应地显示9。这是演示。访问 jsbin/UVOFeGIG/1/edit

I have a small basic html code with JavaScript . All I want is when I type 3,it should show me an image as specified and for 9 accordingly. This is the demo. Visit jsbin/UVOFeGIG/1/edit

它在那里工作。我不知道为什么JavaScript不能这样工作。任何人都可以想出来吗?

Its working there. I don't know why the JavaScript doesn't work this way. Can anyone figure this out?

<!DOCTYPE html> <html> <head> <script src="ajax.googleapis/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <script type="text/javascript"> var num2img = { "3":"visa", "9":"mastercard" }; $('#num').on('input', function(){ var val = this.value; if(val.length<=1){ var n = this.value.charAt(0); if(val && num2img[n]!==undefined){ $('#cardImage')[0].src = 'placehold.it/100x100/eee&text='+ num2img[n] +'.png'; }else{ $('#cardImage')[0].src = 'placehold.it/100x100/cf5'; } } }); </script> <meta charset=utf-8 /> <title>Demo by Roko C.B.</title> </head> <body> <input id="num"><br> <img id="cardImage" src="placehold.it/100x100/cf5"> </body> </html>

推荐答案

试试这个

Try this

$(document).ready(function(){ var num2img = { "3":"visa", "9":"mastercard" }; $('#num').keyup(function(){ var val = this.value; if(val.length<=1){ var n = this.value.charAt(0); if(val && num2img[n]!==undefined){ $('#cardImage')[0].src = 'placehold.it/100x100/eee&text='+ num2img[n] +'.png'; }else{ $('#cardImage')[0].src = 'placehold.it/100x100/cf5'; } } }); });

DEMO

更多推荐

输入时更改图像

本文发布于:2023-11-11 22:46:53,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!