jquery下拉框联动 获取.ashx数据加载到下拉框

来源:互联网 发布:网络不好怎么办 编辑:程序博客网 时间:2024/05/16 12:31

   jquery下拉框联动 获取.ashx数据加载到下拉框 

<script src="../Resource/media/js/jquery-1.11.1.min.js"></script> 

<div>

            <table>
                <tr>
                    <td>
                        <asp:Label ID="lblTaskType" runat="server" Text="任务类型"></asp:Label>:</td>
                    <td>
                        <select id="ddlTaskType" name="ddlTaskType" runat="server">
                            <option value="0">-请选择-</option>
                            <option value="4">月度计划</option>
                            <option value="3">专项计划</option>
                            <option value="2">协作任务</option>
                            <option value="1">工作安排</option>
                        </select>
                    </td>
                    <td>
                        <asp:Label ID="lblTaskName" runat="server" Text="任务名称"></asp:Label>:
                    </td>
                    <td>
                        <select id="ddlTaskName" name="ddlTaskName" runat="server">
                            <option value="0">-请选择-</option>
                        </select>
                    </td>
                </tr>


            </table>

        </div>


<script type="text/javascript">
    $(function () {
        $("#ddlTaskType").bind("keyup change", function (e) {
            e.preventDefault();
            // 首先初始化
            $("#ddlTaskName").empty().append($("<option></option>").val("0").html("-请选择-"));
            if ($(this).val() != "0") {
                sendData($(this).val());
            }
        });
        function sendData(taskTypeID) {
            $.ajax({
                type: "GET",
                url: "ttGetIsCanAddTask.ashx",
                data: { TaskType: taskTypeID },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    for (var i = 0; i < msg.data.length; i++) {
                        $("<option value='" + msg.data[i].id + "'>" + msg.data[i].taskname + "</option>").appendTo($("#ddlTaskName"))//动态添加Option子项
                    }
                },
                error: function () {
                    alert("ajax请求发生错误");
                }
            });
        }
    });
</script>


-----------------------------------------------------------------------------------------------------ttGetIsCanAddTask.ashx-----------------------------------------------------------------------------------------------


  public class ttGetIsCanAddTask : IHttpHandler
    {


        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.QueryString["TaskType"] != null)
            {
                string TaskType = context.Request.QueryString["TaskType"].ToString();
                string adaccount = ADHelper.GetCurrentUser();
                SqlParameter[] parameter = { new SqlParameter("@DutyMan", SqlDbType.NVarChar),
                                           new SqlParameter("@TaskType", SqlDbType.NVarChar)};
                parameter[0].Value = adaccount;
                parameter[1].Value = TaskType;
                DataSet ds = DbHelperSQL.RunProcedure("tSP_GetIsCanAddTask", parameter, "data");
                IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
                timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
                string jsonStr = JsonConvert.SerializeObject(ds, timeFormat, new DataTableConverter());
                context.Response.ContentType = "text/plain";
                context.Response.Write(jsonStr);
            }
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

0 0
原创粉丝点击