Jquery 在 MVC Foreach 中制作选择器

标签 jquery css asp.net-mvc jquery-selectors

我正在尝试实现评级插件。我能够让评级插件正常工作。我正在尝试扩展它。因此,每次点击评级时,它都会将标签置于评级本身之下。目前它绑定(bind)到页面其他地方的 css id。

该场景是一条 Twitter 消息,似乎无法让选择器更新消息本身。它将信息传递给所有人,因为它不是唯一的。

如何使元素独一无二。该插件的文档是 http://rateit.codeplex.com/documentation .

 $(document).ready(function () {

      //we bind only to the rateit controls within the products div

      $(".rateit").bind('over', function (event, value) { $(this).attr('title', value); });
      $(" .rateit").bind('rated reset', function (e) {

          var ri = $(this);

          //if the use pressed reset, it will get value: 0 (to be compatible with the HTML range control), we could check if e.type == 'reset', and then set the value to  null .
          var value = ri.rateit('value');
        var ratingid = ri.data('ratingid');           



          //maybe we want to disable voting?
          ri.rateit('readonly', true);

          $.ajax({
              url: '/Home/GetRating', //your server side script
              data: { id: ratingid, value: value }, //our data
              type: 'POST',
              success: function (data) {
                  $('#ratemsg').html(data);

              },
              error: function (jxhr, msg, err) {
                  $('#response').append('<li style="color:red">' + msg + '</li>');
              }
          });
      });

  });

</script>

 @foreach (var item in Model)
{ 
 <div id="TwitterMessageBox@(item.StatusId.ToString())" class="TwitterMessageBox">

    <ol>
        <li class="TweetLineBody">
            @Html.Hidden("Statusid", item.StatusId)
            <div class="TwitProfileImage">
                <img src=@Url.Content(item.ImageUrl) alt="@item.ScreenName" />
            </div>
            <div class="TwitRealName">
                @item.User
            </div>
            <div class="TwitNickName">
                @MvcHtmlString.Create(@Html.Namify(@item.ScreenName))
            </div>
            <div class="TweetText">
                @MvcHtmlString.Create(@item.Tweet)</div>
            <div class="TweetTime">
                @Html.RelativeDate(@item.Created)</div>

            <div id="TweetMessageRating@(item.StatusId.ToString())" name="TweetMessageRating@(item.StatusId.ToString())"  class="TweetRating">

               // This is where the rating widget goes and records and updates              
                <div data-ratingid="@item.StatusId"   class="rateit"></div>
         // this is where i want to go but i cant figure out a way
                 <span id ="ratemsg" name="TweetRating@(item.StatusId.ToString())"  class="ratemsg"> </span>
             </div>

        </li>
    </ol>

  </div>
 }

正如您在上面看到的,我试图通过将作为状态 ID 的消息 ID 添加到选择器来设置唯一键,但我似乎无法使用通配符选择它。我尝试了 jquery 站点上的一些示例,但似乎无法理解。

最佳答案

不使用 id,只保存当前 .rateit 元素的兄弟元素。

var ri = $(this),
    msg = ri.next();

 ...

    success: function (data) {
        msg.html(data);
    },

关于Jquery 在 MVC Foreach 中制作选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10153757/

相关文章:

javascript - 使用 .next 从一个 UL 遍历到另一个 UL

android - css 媒体查询 - 如何仅针对 ios 或 android 设备?

c# - EF 代码第一个一对多

javascript - 如何获取面板上生成的行的点击事件?

javascript - 如何在不使用井号 (#) 的情况下通过 url 打开新页面并滚动到新页面的 div?

javascript - Jquery .text().length 计算元素的代码和字符?

html - 如何自动将大量文本格式化为html?

javascript - 关于 prestashop 的 cdn 的特殊 css 和 js url

c# - 检查 HtmlHelperMethod 中是否存在(部分) View

asp.net-mvc - 刷新asp.net mvc页面