【问题标题】:Client side changes of div title are not being reflected in C# code behinddiv 标题的客户端更改未反映在后面的 C# 代码中
【发布时间】:2013-10-11 07:54:48
【问题描述】:

我的 div 元素标题有问题。以下是我的 div 元素:

<div id="sampleDiv" runat="server" class="exStyle" onclick="return div_Click(this);" title="any"></div>

以下是我的javascript:

function div_Click(btn) {
    if (btn.title == "any") {
        btn.title = "on";
        btn.style.background = "url('sample1.png')";
    }
    else if (btn.title == "on") {
        btn.title = "off";
        btn.style.background = "url('sample2.png')";
    }
    else if (btn.title == "off") {
        btn.title = "any";
        btn.style.background = "url('sample3.png')";
    }
    return false;
}

在客户端将标题更改为“on”或“off”后,后面c#代码中Attributes["title"]的值仍然是“any”!!任何帮助家伙...

【问题讨论】:

    标签: c# javascript jquery asp.net css


    【解决方案1】:

    你想做什么?由于标题是属性,因此它永远不会发回服务器。只有输入元素被发送回服务器。因此,您无法获得更改后的值。

    要绕过此限制,您必须添加带有 runat 服务器标签的隐藏字段。在发布之前将 hiddenfield 值更新为 div 标题,并使用 hiddenfield 恢复代码中的值。价值。

    希望这很清楚。

    【讨论】:

      【解决方案2】:

      试试这个,对你有帮助

      $('#sampleDiv').click(function() {
          if ($(this).prop('title') == "any") {
              $(this).prop('title',"on");
              $(this).prop('background,url(sample1.png)');
          }
          else if ($(this).prop('title') == "on") {
               $(this).prop('title',"off");
              $(this).prop('background,url(sample2.png)');
          }
          else if ($(this).prop('title') == "off") {
               $(this).prop('title',"any");
              $(this).prop('background,url(sample3.png)');
          }
      });
      

      小提琴Here

      【讨论】:

      • 能否请您发布 C# 方式来访问 div 标题,除了 Attributes["title"]...这是我想要的吗??
      • myDiv.Attributes["title"] = "some title";
      • 首先在 div 中添加 runnat 标记,因为在 c# 中每个元素只能由它访问。然后使用 myDiv.Attributes["title"] = "some title";用于将值放入 div 标题并在某些变量中获取 myDiv.Attributes["title"]
      • 这对您有用吗?
      • 什么都试过了,没用我的朋友!当我尝试从后面的 c# 代码访问值时,标题仍然保持原样......在 UI 中,它正在生效意味着工具提示正在更改,但基于该提示,我返回了一个没有得到的布尔值它的价值!
      【解决方案3】:

      最好在此处使用 switch 语句。如果有匹配就会中断

       function div_Click(btn) {
           btn=$(btn);
           switch(btn.attr('title'))
              {
              case 'any':
                btn.attr('title','on');
                btn.css('background','url(sample1.png)');
                break;
              case 'on':
                btn.attr('title','off');
                btn.css('background','url(sample2.png)');
                break;
              case 'off':
                btn.attr('title','on');
                btn.css('background','url(sample3.png)');
                break;
              default:
                //default code if no match
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-26
        • 1970-01-01
        • 1970-01-01
        • 2021-04-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-31
        • 1970-01-01
        相关资源
        最近更新 更多