jQuery:提交前修改隐藏表单字段值
我在部分视图中有以下代码(使用 Spark):
<span id="selectCount">0</span> video(s) selected.
<for each="var video in Model">
<div style="padding: 3px; margin:2px" class="video_choice" id="${video.YouTubeID}">
<span id="video_name">${video.Name}</span><br/>
<for each="var thumb in video.Thumbnails">
<img src="${thumb}" />
</for>
</div>
</for>
# using(Html.BeginForm("YouTubeVideos","Profile", FormMethod.Post, new { id = "youTubeForm" }))
# {
<input type="hidden" id="video_names" name="video_names" />
<input type="submit" value="add selected"/>
# }
<ScriptBlock>
$(".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);
</ScriptBlock>
本质上,我显示了一系列包含从 YouTube API 提取的信息的 div。我使用 jQuery 允许用户选择他们想要添加到他们的个人资料中的视频。当我提交表单时,我想用逗号分隔的视频 ID 列表填充隐藏字段。一切正常,除了当我尝试设置字段的值时,在发布的控制器中,该字段返回为空。我正在使用 jQuery ajax 表单插件。
我做错了什么,不允许将我在字段中设置的值发送到服务器?
I have the following code in a partial view (using Spark):
<span id="selectCount">0</span> video(s) selected.
<for each="var video in Model">
<div style="padding: 3px; margin:2px" class="video_choice" id="${video.YouTubeID}">
<span id="video_name">${video.Name}</span><br/>
<for each="var thumb in video.Thumbnails">
<img src="${thumb}" />
</for>
</div>
</for>
# using(Html.BeginForm("YouTubeVideos","Profile", FormMethod.Post, new { id = "youTubeForm" }))
# {
<input type="hidden" id="video_names" name="video_names" />
<input type="submit" value="add selected"/>
# }
<ScriptBlock>
$(".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);
</ScriptBlock>
Essentially i display a series of divs that contain information pulled from the YouTube API. I use jQuery to allow the the user to select which videos they would like to add to their profile. When i submit the form i would like to populate the hidden field with a comma separated list of video ids. Everything works except that when i try to set the value of the field, in the controller on post, the field comes back empty. I am using the jQuery ajax form plugin.
What am i doing wrong that is not allowing the value i set in the field to be sent to the server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为您的 beforeSubmit 尝试一下:
Give this a try instead for your beforeSubmit: