日期:2014-05-16  浏览次数:20450 次

我需要给所有的jsp页面中的一个文本框加上控制事件
如题
比如: aaa<Input type=text name="bbb" value="" />
我需要给aaa中如果输入zhaowei 12345/KF/huawei01,都被截取成zhaowei 12345


js或者jquery

------解决方案--------------------
“遍历”所有的jsp页面是不现实的。
你可以在每个页面中都引入一个js文件。
<script src="a.js"></scirpt>
然后在js代码如下,脱离焦点以后截取。
HTML code

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<input name="bbb" />
<script>
list=document.getElementsByName("bbb");
if(list.length>0)
{
   for(var i=0;i<list.length;i++)
   {
        list[i].onblur=function(){
            var value=this.value;
            var pos=value.indexOf("/");
            if(pos != -1)
            {
                this.value=value.slice(0,pos);
            }
        };
   }
}
</script>
</body>
</html>

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=gbk">
        <title>Untitled Document</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
        <script type="text/javascript">
            $(function(){
                var flagObj = 'zhaowei 12345/KF/huawei01';
                $("input[type='text']").bind('blur',function(){
                    if($(this).val() == flagObj){
                        var tempValue = $(this).val().substring(0,$(this).val().indexOf("/",0));
                        $(this).val(tempValue);
                    }
                });
            });
        </script>
    </head>
    <body>
        <div>
            AAA:<input type="text" name="name_a" value="">
            <br>
            BBB:<input type="text" name="name_b" value="">
            <br>
            CCC:<input type="text" name="name_c" value="">
            <br>
            
            
        </div>
    </body>
</html>