当前位置: 代码迷 >> JavaScript >> TEXT验证是否为空
  详细解决方案

TEXT验证是否为空

热度:239   发布时间:2012-04-24 14:15:38.0
求助TEXT验证是否为空
JScript code
        <input id="Text1" name="txt1" type="text" onblur="return text_onblur"/>  
        <img id="img1" alt="" src="3869245_175711061690_2.jpg" style="width: 22px; height: 20px" />
        <input id="Text7" readonly="readonly" type="text" value="必须填写" class="txt" /><br />

我知道用onblur事件,但是我想要在return text_onblur函数里改变img1的图片名,显示另外一张图片,改变TEXT7的VALUE
写成“此项必须填写”怎么做呢?
JScript code
y = document.getElementById(("Text" + name));
            aimg = "img" + name;
            atext="Text"+(name+6);
            if (y.value == "") {
                aimg.src = "禁止.jpg";
                aimg.style = "width: 22px; height: 20px";
                atext.value = "此项不能为空";
            }

我这样写没用,分不多,求帮助

------解决方案--------------------
HTML code

楼主可以试试下面的代码:

<input id="Text1" name="txt1" type="text" onblur="return onText1_Blur(this);"/>  
        <img id="img1" alt=""  style="width: 100px; height: 100px" />
        <input id="Text7" readonly="readonly" type="text" value="必须填写" class="txt" /><br />


<script>
var img = document.getElementById('img1');
function onText1_Blur(sender,args){
    img.src = 'http://avatar.profile.csdn.net/F/A/A/2_w362435819.jpg';    
}

</script>

------解决方案--------------------
HTML code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script>
        function text_onblur(name) {
            
            y = document.getElementById(("Text" + name));
            aimg = "img" + name;
            atext = "Text" + (name + 6);
            if (y.value == "") {
                document.getElementById(aimg).src = "禁止.jpg";
                document.getElementById(aimg).style = "width: 22px; height: 20px";
                document.getElementById(atext).value = "此项不能为空";
            }
        }
    </script>
</head>
<body>
<input id="Text1" name="txt1" type="text" onblur="text_onblur(1)"/>  
        <img id="img1" alt="" src="3869245_175711061690_2.jpg" style="width: 22px; height: 20px" />
        <input id="Text7" readonly="readonly" type="text" value="必须填写" class="txt" /><br />
</body>
</html> 
  相关解决方案