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

2023-09-11 01:16:56 作者:ˉ你是我爱的唯一ゝ

我有以下的code在局部视图(使用星火):

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>

基本上我展示了一系列包含来自YouTube的API拉到信息的div。我使用jQuery允许用户选择其中的视频,他们想加入到他们的个人资料。当我提交表单我想填充隐藏字段的视频ID的逗号分隔的列表。一切正常,只是当我尝试设置字段的值,在后期的控制器,现场回来空。我使用jQuery的阿贾克斯形式的插件。

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?

推荐答案

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

Give this a try instead for your beforeSubmit:

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