【问题标题】:Take value from radio button to choose an image in html从单选按钮中获取值以在 html 中选择图像
【发布时间】:2016-03-09 21:00:48
【问题描述】:

我有一个聊天应用程序。这是在聊天页面中显示对话的代码:

<div id="conversation">
  <div id="messages" class="scrollbar col-md-6">
    <ul ng-repeat="(k,v) in messageshistory " class="chat" data-ng-show="k == tmpuserid">
      <li ng-repeat="(a,b) in v ">
        <span ng-class="(b.from == tmpuserid) ? 'chat-img pull-right':'chat-img pull-left'">
          <img src="./img/avatar.png" alt="User Avatar" class="img-circle">
        </span>
          <div class="col-md-10 chat-body clearfix" >
            <div class="header">
              <small data-ng-if="b.from == tmpuserid" class="text-muted">
                <span  class="glyphicon glyphicon-time"></span> {{ b.time }}
              </small>
                <strong ng-class="(b.from == tmpuserid) ? 'pull-right primary-font':'primary-font'" >{{b.fromname}}</strong>
              <small data-ng-if="b.from != tmpuserid" class="pull-right text-muted">
                <span  class="glyphicon glyphicon-time"></span>{{ b.time }}
              </small>
            </div>
                <p>{{ b.message }}</p>
         </div>
       </li>
     </ul>
   </div>
 </div>

所以在这个应用程序中,注册页面没有为他们的头像上传照片。所以我使用 <img src="./img/avatar.png" alt="User Avatar" class="img-circle">

但我想用单选按钮在注册页面中添加“性别”,例如:

<form>
  <input type="radio" name="gender" value="male" checked> Male<br>
  <input type="radio" name="gender" value="female"> Female
</form>

当用户在注册中检查“男性”时,他将使用 <img src="./img/maleavatar.png" alt="Male Avatar" class="img-circle">

当用户选中“女性”时,她将使用 <img src="./img/femaleavatar.png" alt="Female Avatar" class="img-circle">

我的意思是,这个应用程序会有两个头像,如果男性使用男性,如果女性使用女性,我该怎么写?

【问题讨论】:

  • @demo 但在我的情况下,图像选项在其他页面中,而不是在同一页面中

标签: javascript html image


【解决方案1】:

您可以将这两个图像都添加到您的 HTML 中,然后在用户性别上使用带有 ng-switch 的正确图像(ng-switch 和单选按钮绑定上有足够的来源供您找到解决方案)。

希望这能让你走上正轨,如果不只是回问的话。

【讨论】:

  • 我认为它可能是答案,但“性别”在“注册”页面中,“头像”在“聊天”页面中。在进入聊天页面之前,用户必须先登录。所以,这个数据会跳转很多页。我可以用这个吗?
  • 让你的数据在服务中保持良好和紧密(登录时从服务器获取数据,将它们存储在服务内部的变量中,并通过注入服务从任何地方访问它们),这不会一个问题。 (使用 $http/$resource 逻辑扩展此 tutorial 中的方法。)
猜你喜欢
  • 1970-01-01
  • 2011-10-28
  • 2013-09-28
  • 2013-03-19
  • 2018-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-15
相关资源
最近更新 更多