当前位置:编程学习 > C#/ASP.NET >>

[Javascript问题]Jquery使用中遇到的小问题 望不吝赐教


window.onfocus = function(){
$(":input[id^='btnOpration']").each(function(){
$(this).click(function(){
var val = $(":input[id$='txtExpressions']").val();
switch($(this).val())
{
case "←":
$(":input[id$='txtExpressions']").val(val.length>0?val.substring(0,val.length-1):"");
break;
case "CE":
$(":input[id$='txtExpressions']").val("");
break;
case "等于":
$(":input[id$='txtExpressions']").val(val+"=");
break;
case "小于":
$(":input[id$='txtExpressions']").val(val+"<");
break;
case "大于":
$(":input[id$='txtExpressions']").val(val+">");
break;
default:
//alert($(this).val());
$(":input[id$='txtExpressions']").val(val+(isField($(this).val())?"["+$(this).val()+"]":$(this).val()));
break;
}
});
});
}

function isField(val)
{
var params = new Array("0","1","2","3","4","5","6","7","8","9","+","-","*","/",".","(",")");
params = params.join(",");
if(params.indexOf(val)>=0)
{
return false;
}
return true;
}



<table cellpadding="0" cellspacing="0" border="1" width="98%" class="tablesort" style="MARGIN-TOP:0px;MARGIN-BOTTOM:0px">
<tr>
<td width="75%" style="MARGIN-LEFT:10px">
<asp:TextBox ID="txtExpressions" Runat="server" TextMode="MultiLine" Width="100%" Rows="6"></asp:TextBox></td>
<td style="VERTICAL-ALIGN:top">
<table class="opration">
<tr>
<td><input type="button" id="btnOpration10" value="1"></td>
<td><input type="button" id="btnOpration11" value="2"></td>
<td><input type="button" id="btnOpration12" value="3"></td>
<td><input type="button" id="btnOpration13" value="+"></td>
<td><input type="button" id="btnOpration14" value="←"></td>
</tr>
<tr>
<td><input type="button" id="btnOpration20" value="4"></td>
<td><input type="button" id="btnOpration21" value="5"></td>
<td><input type="button" id="btnOpration22" value="6"></td>
<td><input type="button" id="btnOpration23" value="-"></td>
<td rowspan="4" class="maxButton"><input type="button" id="btnOpration24" value="CE" style="HEIGHT:100%"></td>
</tr>
<tr>
<td><input type="button" id="btnOpration30" value="7"></td>
<td><input type="button" id="btnOpration31" value="8"></td>
<td><input type="button" id="btnOpration32" value="9"></td>
<td><input type="button" id="btnOpration33" value="*"></td>
</tr>
<tr>
<td><input type="button" id="btnOpration40" value="0"></td>
<td><input type="button" id="btnOpration41" value="."></td>
<td><input type="button" id="btnOpration42" value="等于"></td>
<td><input type="button" id="btnOpration43" value="/"></td>
</tr>
<tr>
<td><input type="button" id="btnOpration50" value="小于"></td>
<td><input type="button" id="btnOpration51" value="大于"></td>
<td><input type="button" id="btnOpration52" value="("></td>
<td><input type="button" id="btnOpration53" value=")"></td>
</tr>
</table>
</td>
</tr>
</table>



 INPUT { BORDER-BOTTOM: #eee 1px groove; BORDER-LEFT: #eee 1px groove; BACKGROUND-COLOR: #f0f8ff; COLOR: blue; BORDER-TOP: #eee 1px groove; BORDER-RIGHT: #eee 1px groove }
 .opration TD { WIDTH: 50px }
 .opration INPUT { WIDTH: 100%; HEIGHT: 20px }
 .rewidth TD { WIDTH: 150px }
 .rewidth INPUT { WIDTH: 100%; HEIGHT: 20px }
 .maxButton { HEIGHT: 100% }
--------------------编程问答-------------------- 问题是 按钮点击 有时候会出现多次重复值 --------------------编程问答-------------------- 顶一下
  --------------------编程问答-------------------- 两次提交

一种状况会发生在<button/> 标签,而且仅在IE8下会出现,因为IE8下默认 type="submit",这时,只要明确指明type类型就可以

而另一种情况,可能是用户在请求未处理完毕之前,连续点击提交所致,那么,可以用户点击之后,锁定,请求完成,在解锁即可. --------------------编程问答-------------------- 不是这个问题  是点击按钮一次 有时候会出现 1到多次重复值
--------------------编程问答-------------------- 再顶一次 --------------------编程问答-------------------- 有可能是window.onfocus =的问题,改成这个试试

$(function(){
   $(":input[id^='btnOpration']").each(function(){
                $(this).click(function(){
                    var val = $(":input[id$='txtExpressions']").val();
                    switch($(this).val())
                    {
                        case "←":
                            $(":input[id$='txtExpressions']").val(val.length>0?val.substring(0,val.length-1):"");
                            break;                        
                        case "CE":
                            $(":input[id$='txtExpressions']").val("");
                            break;
                        case "等于":
                            $(":input[id$='txtExpressions']").val(val+"=");
                            break;
                        case "小于":
                            $(":input[id$='txtExpressions']").val(val+"<");
                            break;
                        case "大于":
                            $(":input[id$='txtExpressions']").val(val+">");
                            break;
                        default:
                            //alert($(this).val());                                
                            $(":input[id$='txtExpressions']").val(val+(isField($(this).val())?"["+$(this).val()+"]":$(this).val()));
                            break;
                    }                                                    
                });                            
            });
}); --------------------编程问答-------------------- 你把方法写在 window.onfocus 中,每次你的按钮获得焦点时都会触发。 --------------------编程问答-------------------- 所以你会得到多次的值 --------------------编程问答--------------------
引用 7 楼 yw39019724 的回复:
你把方法写在 window.onfocus 中,每次你的按钮获得焦点时都会触发。

+++ --------------------编程问答-------------------- 6楼的以下都没错 确实是 focus的问题 --------------------编程问答--------------------
引用 2 楼 shwicho 的回复:
顶一下


引用 10 楼 shwicho 的回复:
6楼的以下都没错 确实是 focus的问题



顶一下 --------------------编程问答--------------------
引用 11 楼 net_HCC 的回复:
引用 2 楼 shwicho 的回复:顶一下

引用 10 楼 shwicho 的回复:6楼的以下都没错 确实是 focus的问题


顶一下
0 --------------------编程问答-------------------- focus 用的时候还是要注意的。 
搞明白就好。 --------------------编程问答-------------------- 这个知识点 学到了 --------------------编程问答--------------------  其实吧 你应该先清掉$(":input[id$='txtExpressions']").val("");的值 

 然后在去执行循环累加的   

把上面那句话加到第一行 你那个onfocs不保险 一般鼠标的移进去移出来很正常的 每次都在叠加 重复了也正常 --------------------编程问答--------------------
积累js知识中。。。。。。。 --------------------编程问答-------------------- js错误 有时调试一下 很快就发现问题出哪里了,, --------------------编程问答-------------------- 继续积楼 结贴
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,