JQuery的 - 通过元素的坐标ASP.NET code后面? [英] JQuery - Pass coordinates of element to ASP.NET code behind?

查看:113
本文介绍了JQuery的 - 通过元素的坐标ASP.NET code后面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建将使用所提供的偏移位置功能简单的ASP.NET站点通过jQuery来传递&LT的坐标; DIV> 通过一个按钮的的OnClick元素为后面的ASP.NET code 方法。

I'm trying to create a simple ASP.NET site which will use the offset or position functions provided by JQuery to pass the coordinates of a <div> element to an ASP.NET code behind via a button's OnClick method.

我搜查,发现这个例子但它似乎不工作正如所料,在单击没有坐标返回。

I've searched and found this example but it doesn't seem to work as expected, upon clicking no coordinates are returned.

我如何获得给定&LT的坐标; DIV&GT; 元素,并通过这些到ASP.NET按钮的的OnClick 方法?

How do I obtain the coordinates of a given <div> element and pass these to an ASP.NET button's OnClick method?

推荐答案

第1部分

要获得元素的位置,您可以使用偏移()或的位置()

小提琴: http://jsfiddle.net/XFfLP/

function test() {
    var p = $("#testID");
    var position = p.offset();//p.position()
    $("#Field1").val(position.top);
    $("#Field2").val(position.left);
}​



第2部分

若要从页的数据传递到服务器code-后面你可以使用网​​络方法

文章:的http://blog.nitinsawant.com/2011/09/draft-sending-client-side-variables-to.html

1 网络法样品code:

1. Web-Method sample code:

[System.Web.Services.WebMethod]
 public static string AcceptData(object jsonData)
 {
     Customer newCust =(Customer)JsonConvert.DeserializeObject(jsonData.ToString(),typeof(Customer));
     return "Server response: Hello "+newCust.FirstName;
 }

2 JS样本code:

var newCustomer = {
    "FirstName": $("#txtFirstName").val(),
    "LastName": $("#txtLastName").val(),
    "Telephone": $("#txtTelephone").val()
}

var jsonData = "{'jsonData':'" + JSON.stringify(newCustomer) + "'}";//create string representation of the js object

        //post data to server
        $.ajax({
            type: "POST",
            url: 'Test.aspx/AcceptData',
            data: jsonData,
            contentType: "application/json; charset=utf-8",
            dataType: ($.browser.msie) ? "text" : "json",
            success: function(msg) {
                //call successfull
                var obj = msg.parseJSON();
                alert(obj.d); //d is data returned from web services

                //The result is wrapped inside .d object as its prevents direct execution of string as a script
            },
            error: function(xhr, status, error) {
                //error occurred
                alert(xhr.responseText);
            }
        });

这篇关于JQuery的 - 通过元素的坐标ASP.NET code后面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆