• AJAX校验商品价格(类似校验用户名)


    服务器端程序

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    <%@ WebHandler Language="C#" Class= "GetPrice" %>
     
    using System;
    using System.Web;
     
    public class GetPrice : IHttpHandler {
        
        public void ProcessRequest (HttpContext context) {
            context .Response.ContentType = "text/plain";
            string name = context.Request ["name"];
            
            DataSetProductsTableAdapters .T_ProductsTableAdapter priceadapter=new DataSetProductsTableAdapters .T_ProductsTableAdapter();
            DataSetProducts .T_ProductsDataTable prices = priceadapter.GetDataByName(name);
            if ( prices.Count <=0)
            {
                context .Response.Write("none|0" );
            }
            else
            {
                DataSetProducts .T_ProductsRow price = prices[0 ];
                context .Response.Write("ok|" +Convert.ToString(price .Price));
            }
        }

    客户端页面代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    <script type="text/javascript">
            $( function () {
                $( "#Text1").blur(function () {
                    var sname = $("#Text1" ).val();
                    $.post( "GetPrice.ashx", { name: sname }, function (data, status) {
                        if (status == "success" ) {
                            var arrs = data.split("|" );
                            if (arrs[0] == "ok" ) {
                                $( "#Text2").val(arrs[1]);
                            }
                            else if (arrs[0] == "none") {
                                alert( "没有该商品" );
                            }
                            else {
                                alert( "AJAX错误");
                            }
                        }
                        else {
                            alert( "wrong");
                        }
     
                    });
                });
            })
        </script >
     
    <body>
     
        <p >
            <input id="Text1" type="text" />
            <input id="Text2" type="text" /></p>
     
    </body>
    </html>

    测试效果


  • 相关阅读:
    搭建docker私有registory (harbor)
    消息队列原理及ActiveMQ、RocketMQ、RabbitMQ、Kafka区别总结
    too many users are authenticated
    docker环境部署nginx、tomcat、redis
    docekr环境部署mysql、kafaka、kafkamanager和mongodb
    在IE6 情况下让PNG图片透明的3种方法
    ecshop商品列表页出现一个空的搜索结果
    ecshop去掉国家省市区,实现手动填写收货地址
    ecshop始终显示全部分类
    ecshop商品列表页增加/显示货号SN
  • 原文地址:https://www.cnblogs.com/zhxshseu/p/5285353.html
Copyright © 2020-2023  润新知