使用 jQuery 更改标签的值

发布于 2024-09-26 10:56:50 字数 2036 浏览 1 评论 0 原文

我有一个标签,costLabel。

我想要做的是根据下拉列表的选定值更改此标签的值。

这是我的 HTML:

<table>
  <tr>
    <td width="343">Package*</td>

    <td colspan="4">
      <select class="purple" name="package">
        <option value="standard">Standard - &euro;55 Monthly</option>
        <option value="standardAnn">Standard - &euro;49 Monthly</option>            
        <option value="premium">Premium - &euro;99 Monthly</option>
        <option value="premiumAnn" selected="selected">Premium - &euro;89 Monthly</option>            
        <option value="platinum">Platinum - &euro;149 Monthly</option>
        <option value="platinumAnn">Platinum - &euro;134 Monthly</option>            
      </select>
    </td>

  <tr>
    <td width="343">
      <p>We bills quarterly/annually in advance</p>
      <p>See <a href="#dialog" name="modal">Pricing</a> for more details</p>
    </td>
    <td colspan="4"><label id="costlabel" name="costlabel">Total Cost:</label></td>

  <tr>
</table>

成本标签中的值是

  • Standard =“€165 Quarterly”
  • StandardAnn =“€588 Annually”
  • Premium =“€297 Quarterly”
  • PremiumAnn =“€1068 Annually”
  • Platinum =“€447 Quarterly”
  • PlatinumAnn =“每年 €1608”

我确实有以下代码,根据下拉菜单计算费用,但注册表已更改为更简单(即折扣选择没有消失),我有点适应 jQuery 时遇到麻烦。有人可以帮忙吗?

$(function() {
  $("#discountselection, select[name=package], input[name=discount]").
    change(function() {
      var
        selected_value = $("#discountselection").val(),
        discount = [12, 24, 36],
        package = $("select[name=package]").val(),
        package_prices = {'standard': 49, 'premium': 85, 'platinum': 134 },
        cost = package_prices[package] * discount[selected_value-1];

      $("#costlabel").val(cost);
    });
});

I have a label, costLabel.

What I want to be able to do is change the value of this label depending on the selected value of a dropdownlist.

This is my HTML:

<table>
  <tr>
    <td width="343">Package*</td>

    <td colspan="4">
      <select class="purple" name="package">
        <option value="standard">Standard - €55 Monthly</option>
        <option value="standardAnn">Standard - €49 Monthly</option>            
        <option value="premium">Premium - €99 Monthly</option>
        <option value="premiumAnn" selected="selected">Premium - €89 Monthly</option>            
        <option value="platinum">Platinum - €149 Monthly</option>
        <option value="platinumAnn">Platinum - €134 Monthly</option>            
      </select>
    </td>

  <tr>
    <td width="343">
      <p>We bills quarterly/annually in advance</p>
      <p>See <a href="#dialog" name="modal">Pricing</a> for more details</p>
    </td>
    <td colspan="4"><label id="costlabel" name="costlabel">Total Cost:</label></td>

  <tr>
</table>

The values that go into the cost label are

  • Standard = "€165 Quarterly"
  • StandardAnn = "€588 Annually"
  • Premium = "€297 Quarterly"
  • PremiumAnn = "€1068 Annually"
  • Platinum = "€447 Quarterly"
  • PlatinumAnn = "€1608 Annually"

I did have the following code in place which calculated the cost depending on the dropdown menu, but the registration form has since changed to be more simpler(i.e. discountselection is not gone), and I'm having a bit of trouble adapting the jQuery. Can someone help?

$(function() {
  $("#discountselection, select[name=package], input[name=discount]").
    change(function() {
      var
        selected_value = $("#discountselection").val(),
        discount = [12, 24, 36],
        package = $("select[name=package]").val(),
        package_prices = {'standard': 49, 'premium': 85, 'platinum': 134 },
        cost = package_prices[package] * discount[selected_value-1];

      $("#costlabel").val(cost);
    });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撩动你心 2024-10-03 10:56:50

我似乎对你的 html 结构有一个盲点,但我认为这就是你正在寻找的。它应该从 select 输入中找到当前选定的选项,将其文本分配给 newVal 变量,然后然后将该变量应用于 #costLabel 标签的 >value 属性:

jQuery

$(document).ready(
  function() {
    $('select[name=package]').change(
      function(){
        var newText = $('option:selected',this).text();
        $('#costLabel').text('Total price: ' + newText);
      }
      );
  }
  );

html:

  <form name="thisForm" id="thisForm" action="#" method="post">
  <fieldset>
    <select name="package" id="package">
        <option value="standard">Standard - €55 Monthly</option>
        <option value="standardAnn">Standard - €49 Monthly</option>            
        <option value="premium">Premium - €99 Monthly</option>
        <option value="premiumAnn" selected="selected">Premium - €89 Monthly</option>            
        <option value="platinum">Platinum - €149 Monthly</option>
        <option value="platinumAnn">Platinum - €134 Monthly</option>   
    </select>
  </fieldset>
    
    <fieldset>
      <label id="costLabel" name="costLabel">Total price: </label>
    </fieldset>
  </form>

上述内容的工作演示位于:JS Bin

I seem to have a blind spot as regards your html structure, but I think that this is what you're looking for. It should find the currently-selected option from the select input, assign its text to the newVal variable and then apply that variable to the value attribute of the #costLabel label:

jQuery

$(document).ready(
  function() {
    $('select[name=package]').change(
      function(){
        var newText = $('option:selected',this).text();
        $('#costLabel').text('Total price: ' + newText);
      }
      );
  }
  );

html:

  <form name="thisForm" id="thisForm" action="#" method="post">
  <fieldset>
    <select name="package" id="package">
        <option value="standard">Standard - €55 Monthly</option>
        <option value="standardAnn">Standard - €49 Monthly</option>            
        <option value="premium">Premium - €99 Monthly</option>
        <option value="premiumAnn" selected="selected">Premium - €89 Monthly</option>            
        <option value="platinum">Platinum - €149 Monthly</option>
        <option value="platinumAnn">Platinum - €134 Monthly</option>   
    </select>
  </fieldset>
    
    <fieldset>
      <label id="costLabel" name="costLabel">Total price: </label>
    </fieldset>
  </form>

Working demo of the above at: JS Bin

夜无邪 2024-10-03 10:56:50

val() 更像是 attr('value') 的快捷方式。对于您的用法,请使用 text()html() 代替

val() is more like a shortcut for attr('value'). For your usage use text() or html() instead

心如狂蝶 2024-10-03 10:56:50

.text 是正确的,以下代码对我有用:

$('#lb'+(n+1)).text(a[i].attributes[n].name+": "+ a[i].attributes[n].value);

.text is correct, the following code works for me:

$('#lb'+(n+1)).text(a[i].attributes[n].name+": "+ a[i].attributes[n].value);
等数载,海棠开 2024-10-03 10:56:50

验证 (HTML5):属性“name”不是元素“label”的有效属性。

Validation (HTML5): Attribute 'name' is not a valid attribute of element 'label'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文