2010-04-02 115 views
0

我有一个局部视图以下代码:jQuery的:修改隐藏的表单字段值之前(使用火花)提交

<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表单插件。

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

回答

3

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

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

我想你的建议,但得到了同样的结果:( – 2010-04-02 14:32:47

+1

@Jason - 尝试改变'beforeSubmit:功能(ARR,形式,选择采用)'来'beforeSerialize:function()',你会得到这个值吗? – 2010-04-02 15:19:40

+0

啊,哈!那样做了:)谢谢。 – 2010-04-02 15:35:44

相关问题