2010-04-02 43 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

相關問題