我想实现一个数量和金额之间的关系验证。之前是在服务器端验证的,后来老板说不符合要求,让改成用js验证的,可我对js一窍不通啊,求各位大虾指教,最好有代码啊!公式为:总金额(万元)<=产品数量*7000/10000元,如果不符合要求就给出一个提示!

解决方案 »

  1.   

    <script type ="text/jscript" >
            function checkFormat() {
                var total = document.getElementById("total").value;
                var num = document.getElementById("num").value ;
                if (total <= num * 7000 / 10000) {alert("金额不符合要求!");return false;}
            }
        </script><div>
            请输入总金额:<input type ="text" id ="total"/><br /><br />
            请输入产品数量:<input type ="text" id ="num" /><br />
            <asp:Button runat ="server" ID ="btn_OK" Text ="ok" OnClientClick ="return checkFormat();" />
        </div>
      

  2.   


    我想在输入总金额之后就进行验证,不想再按下提交按钮后才进行验证,您看我这样写对吗?<asp:textbox id="tbPrice" runat="server" Width="120px" ontextchanged="checkFormat()" 
                            ></asp:textbox>   为什么一直报“并不包含checkFormat的定义”呢?
      

  3.   


    function yanzheng() {
        var money = document.getElementById("金额的id");
        var number = document.getElementById("数量的id");
        if (money.value <= number.value * 7000 / 10000) { 
            alert("不符合要求!")
        }
    }
    然后用个事件调用这个方法就行了
      

  4.   

    你那textbox是服务器控件,要在服务器端验证
      

  5.   

    请输入总金额:<input type ="text" id ="total" onblur ="checkFormat()"/>
      

  6.   

    onblur ="checkFormat()"加入到服务器控件TextBox的属性中去.
      

  7.   

    另外,如果是Validaion,服务器也要做验证,客户端验证有可能被绕过去。
      

  8.   

    那就给textbox绑定onblur事件
    <asp:TextBox runat ="server" ID ="total" ></asp:TextBox>protected void Page_Load(object sender, EventArgs e)
        {
            total.Attributes.Add("onblur","checkFormat()");
        }