jQuery:在提交之前修改隐藏的表单字段值

我在局部视图中使用以下代码(使用Spark):

0 video(s) selected.  
${video.Name}
# using(Html.BeginForm("YouTubeVideos","Profile", FormMethod.Post, new { id = "youTubeForm" })) # { # } $(".video_choice").click(function() { $(this).toggleClass('selected'); var count = $(".selected").length; $("#selectCount").html(count); }); var options = { target: '#videos', beforeSubmit: function(arr, form, opts) { var names = []; $(".selected").each(function() { names[names.length] = $(this).attr('id'); }); var namestring = names.join(","); $("#video_names").attr('value',namestring); //alert(namestring); //arr["video_names"] = namestring; //alert($.param(arr)); //alert($("#video_names").attr('value')); return true; } }; $("#youTubeForm").ajaxForm(options);

基本上我会显示一系列div,其中包含从YouTube API中提取的信息。 我使用jQuery允许用户选择他们想要添加到他们的个人资料中的video。 当我提交表单时,我想用逗号分隔的videoID列表填充隐藏字段。 一切正常,但是当我尝试设置字段的值时,在post上的控制器中,字段返回空。 我正在使用jQuery ajax表单插件。

我做错了什么,不允许我在字段中设置的值被发送到服务器?

试试这个,而不是你的beforeSubmit:

 var ids = $(".selected").map(function() { return this.id; }).get().join(','); $("#video_names").val(ids); return true;