【问题标题】:Change <label> based on <select>根据 <select> 改变 <label>
【发布时间】:2016-05-03 02:18:47
【问题描述】:

我已经深入研究了一些类似的问题(这不是一个独特的问题),但许多只处理当前标签或更改标签以匹配选择。

我的 jQuery 技能相当一般:函数很好,但是链接它们(并且知道浏览器技巧......)有点棘手。

我的目标是:

  • 用户从&lt;select&gt; 列表中选择“电子邮件”或“Web”。
  • 基于此选择,我们更新下方&lt;input&gt;&lt;label&gt; 及其下方&lt;span&gt; 的内容(一些帮助文本,将有助于显示“http://yourdomain.com/here”或“stuff@yourdomain. com")

另外:

  • 将输入字段的type 更改为type="email"type="url"。来自what I can see,这可能是因为IE兼容问题?
  • 替代方法可能是 .show()/.hide() 两个输入(每种类型一个),而不是基于选择。

JSFiddlehttp://jsfiddle.net/ys9Cj/(到目前为止:我一直在改变字段。就像我说的,JQuery 不太好!)

这是 HTML:

    <label for="method">How?</label>
    <select id="method" name="method" required>
      <option name="method"  value="email">Email</option>
      <option name="method" value="web">Web</option>
    </select>
    <span class="help-text">The web, or email?</span>
  </label>

  <label for="contact">Email Address</label> // We want to update this (but not strictly with the value from the <select>
  <input type="email" name="contact" id="contact" value="" maxlength="255"> // and the type here, or otherwise .show() or .hide() this and show an alternative field with a different name=""
  <span class="help-text">e.g. stuff@yourdomain.com</span> // and change the contents here

【问题讨论】:

  • 分享你用于上述场景的jquery代码..
  • 为了让人们回答你的问题,你已经尝试简化你的问题..我看到这个问题后的第一印象是它太长了..问题更短更准确。 .更好的反应你会得到..
  • @Onaseriousnote 你的显示名让我笑了:)
  • 尝试创建一个JSFiddle,它会更容易理解,展示你的尝试并得到答案
  • 我添加了一个(部分!)JSFiddle。 @Onaseriousnote - 我试图在这里具体一点,因为我有一些要求。让我不必问一连串的问题! :)

标签: javascript jquery html


【解决方案1】:

试试这个朋友http://jsfiddle.net/Godinall/Pn8HZ/

根据您的选择更改文本,通过数据属性轻松管理

$("#method").change(function() {
    $('#help-text').text($('option:selected').attr('data-content'));
    $('#example-text').text($('option:selected').attr('data-example'));
}).change();

【讨论】:

  • 干杯。我刚刚在输入中添加了data-labeldata-help-text,所以当我显示/隐藏它们时,我可以用这些字符串填充
【解决方案2】:

您的 HTML 和 JavaScript 代码中几乎没有语法错误。请尝试以下操作:

HTML:

<p>
<label for="method">How?</label>
<select id="method" name="method" required>
    <option value="email">Email</option>
    <option value="web">Web</option>
</select> <span class="help-text">The web, or email?</span>
</p>
<p>
<label id="contact-label" for="contact">Email Address</label>
<input type="email" name="contact" id="contact" value="" maxlength="255"> <span class="help-text">e.g. stuff@yourdomain.com</span>
    </p>

JavaScript:

$(document).ready(function(){
    $('#method').change(
        function () {
            var method = $('option:selected', this).text();
            if (method == "Email") {
                $('#contact-label').text("Email Address");
            } else if (method == "Web") {
                $('#contact-label').html("App URL");
            }
        });
 });  

【讨论】:

  • 这帮助很大:非常感谢。我已经设法弄清楚其余的(设置required,显示/隐藏更改时的其他输入)等。唯一的问题是让这个触发不是just更改,但还要确保它适用于页面加载(当我的模板逻辑添加selected
【解决方案3】:

试试这样的,FIDDLE

$(document).ready(function () {
    $('#method').change(
    function () {
        var method = $('option:selected').val();

        if (this.value == "email") {
            $('#contact').text("Email Address");
        } else if (this.value == "web") {
            $('#contact').text("Web Address");
        }
    });

});

【讨论】:

  • 我建议使用数据属性来存储替代文本,将其存储在 Javascript 中并不是最佳选择。除此之外还不错!
  • @qwerty 你能扩展一下吗?我假设您的意思是data-help-text - 在这种情况下,我如何利用它来提取帮助文本?理想情况下,我会在输入上设置它并在显示/隐藏相应输入时替换 #contact-help-text 的内容。
【解决方案4】:

检查这个文件

        $(document).ready(

    $('#method').bind('change',
      function(){
      var method = $('option:selected',this).text();
      alert(method);
      if (method == "Email") {
          $('#contact-label').text("Email Address"); 
     $('#help-change').text("Email Address"); 
          $('#contact').attr('type','email');

      } else if (method == "Web") {
          $('#contact-label').text("App URL"); 
          $('#help-change').text("Web Address"); 
          $('#contact').attr('type','url');
      }

      }
      )

  );

http://jsfiddle.net/ys9Cj/18/

【讨论】:

  • 谢谢:看来我不能在 IE 8 和更早版本中使用 .attr('type','xxx')api.jquery.com/attr
【解决方案5】:
$("#method").change(function () {
    $('#contact').html(($(this).find(':selected').html() == 'Email' ? 'Email Address' : 'Web Address') + ':');       
});

【讨论】:

  • 请为您的答案添加解释
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-16
  • 2016-11-16
  • 2014-04-22
  • 1970-01-01
相关资源
最近更新 更多