<input type="text" id="txt_buchang" name="txt_buchang" onfocus="this.style.color='red'" onblur="this.style.color='#000000'" style="width: 200px" /><input type="text" id="txt_canyin" name="txt_canyin" onfocus="this.style.color='red'" onblur="this.style.color='#000000'" style="width: 200px" />我这里有N多的文本框,我想给所有的文本框一个事件,当用户输入文字时,字体变红色;当离开这个文本框时,字体颜色又变成黑色。还有,怎么提取所有input中的数字,并将有的数字加起来我用的是C# + jquery + html

解决方案 »

  1.   

         $(document).ready(function () {
                //设置默认
                $("input[type='text']").focus(function (obj) {
                    $(this).attr("style", "color:red;");//设置为红色
                });
                $("input[type='text']").blur(function (obj) {
                    $(this).attr("style", "color:#000;");//设置所有恢复黑色
                });
                var sum = 0;
                $("input[type='text']").each(function () {
                    sum += parseInt($(this).val());
                });
                alert(sum);//弹出所有的和
            });
      

  2.   

    颜色变化:onfocus里面统一调用同一个处理方法就好了额求和:把id命名规则点就可以了额 比如 iu+[1...10]取的时候 
    var sum=0;
    for(var i=1;i<=10;i++){
      sum= sum+$("#iu"+i).val();
    }
      

  3.   

    $(document).ready(function () {
                //设置默认
                $("input[type='text']").focus(function (obj) {
                    $(this).attr("style", "color:red;");//设置为红色
                });
                $("input[type='text']").blur(function (obj) {
                    $(this).attr("style", "color:#000;");//设置所有恢复黑色
                });
                var sum = 0;
                $("input[type='text']").each(function () {
                    sum += parseInt($(this).val());
                });
                alert(sum);//弹出所有的和
            });