Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在asp.net中将jQuery变量值发送到服务器端_Javascript_C#_Jquery_Asp.net - Fatal编程技术网

Javascript 如何在asp.net中将jQuery变量值发送到服务器端

Javascript 如何在asp.net中将jQuery变量值发送到服务器端,javascript,c#,jquery,asp.net,Javascript,C#,Jquery,Asp.net,以下是我的jQuery: $(document).ready(function () { var image = $('#man'); var legTooltip = 'LEG'; image.mapster({ fillOpacity: 0.5, mapKey: 'alt', fillColor: "000000", listKey: '

以下是我的jQuery:

    $(document).ready(function () {
        var image = $('#man');

        var legTooltip = 'LEG';

        image.mapster({
            fillOpacity: 0.5,
            mapKey: 'alt',
            fillColor: "000000",
            listKey: 'alt',
            scaleMap: falsee,
            singleSelect: true,
            toolTipClose: ["tooltip-click", "area-click", "area-mouseout"],
            showToolTip: true,
            onClick: function (e) {
                if (e.key === 'leg') {
                   var **symp** = e.key;                        
                }                    
            },
            areas: [
            {
                key: "leg",
                toolTip: legTooltip
            }],
            render_highlight: {
                fade: false
            }                
        });
    });
现在,我如何将变量symp的值发送到服务器端cs文件中,并将该值保存到另一个服务器端变量中?我对jquery和asp.net知之甚少。提前感谢。

试试ajax:

$.(ajax){(
    url : 'url of the file in which yo want to process the data',
    method: 'post or get',
    data: {
        var1 : value1,
        var2 : value2    
        // these variables are available on the specified url  
        // use your variable(symp) here
    },
    success: function(response)
    {
         // response hold the server side response in it.
    }
)}

别忘了在代码中包含jquery库。

如果我没弄错,您需要向服务器发送post请求。您可以使用jQuery方法post(这是一个简写的Ajax函数)来实现这一点

在服务器端,在控制器中添加一个方法,该方法将获得此值

public class YourController : Controller
{
    //other methods and fields

    [HttpPost]
    public ActionResult YourMethod(<type of variable symp> symp)
    {
        //save this value wherever you want
        //return result of your request, so you can proccess it on the client side
        //e.g. that operation was successfully completed
    }
}
公共类控制器:控制器
{
//其他方法和领域
[HttpPost]
公共行动结果方法(symp)
{
//将此值保存到任何需要的位置
//返回请求的结果,以便在客户端进行处理
//e、 g.该操作已成功完成
}
}

如果
symp
的值需要不断更新,那么正如Mayank Pandeys回答的那样,Ajax会更好。 但是如果您只需要回发时的值,那么HiddenField将是最简单的

<asp:HiddenField ID="HiddenField1" runat="server" />

if (e.key === 'leg') {
    var **symp** = e.key;
    document.getElementById("<%= HiddenField1.ClientID %>").value = symp;
}

使用ajax并将该变量值发送到服务器端。感谢您的建议。我开始学习ajax@Divthanks谢谢你的建议。对于我的项目,我需要不断更新。但是你的建议对我也很有帮助,因为我可以在另一个页面上使用它。谢谢…因为我是jquery和ajax新手,你能告诉我这段代码是在哪里写的吗?
<asp:HiddenField ID="HiddenField1" runat="server" />

if (e.key === 'leg') {
    var **symp** = e.key;
    document.getElementById("<%= HiddenField1.ClientID %>").value = symp;
}
string symp = HiddenField1.Value;