javascript 如何在jquery中获取HiddenFor控件的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22007083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:12:24  来源:igfitidea点击:

how to get the value of a HiddenFor control in jquery

javascriptjqueryasp.net-mvc-3razor-2

提问by Rituraj Mishra

i have a hidden for control as

我有一个隐藏的控制作为

@Html.HiddenFor(m => m.SchedulingProfileDetails.Id)

I am trying to access the value in this hidden field in my jquery and then trying to pass the value to controller

我正在尝试访问 jquery 中此隐藏字段中的值,然后尝试将该值传递给控制器

  var id = $("#SchedulingProfile_Id").val();

                        $.ajax({
                            url: rootUrl + 'SchedulingProfile/SaveDetails',
                            type: "POST",
                            data: ({
                                schedulingProfileId: schedulingProfileId, 
                                   });

but I am getting null value in id. Please help me out

但我在 id 中得到空值。请帮帮我

回答by David

I could be wrong, but isn't it merely that:

我可能是错的,但不仅仅是:

var id = $("#SchedulingProfile_Id").val();

needs to be:

需要是:

var id = $("#SchedulingProfileDetails_Id").val();

?

?

回答by VladL

I'ts always good to check the generated HTML page, simply search for hidden input.

检查生成的 HTML 页面总是好的,只需搜索隐藏的输入即可。

Also you are trying to pass schedulingProfileId to your AJAX call, I think it should be:

您还试图将 scheduleProfileId 传递给您的 AJAX 调用,我认为它应该是:

$.ajax({
url: rootUrl + 'SchedulingProfile/SaveDetails',
type: "POST",
data: ({
    schedulingProfileId: id, 
       });