【问题标题】:Changing the text of a value attribute with jQuery使用 jQuery 更改值属性的文本
【发布时间】:2021-04-06 18:10:47
【问题描述】:

我无法直接编辑我需要更改的文本,因此我需要使用 jQuery 覆盖给定值的文本。

我了解基础知识,但不知道找到值的正确字符串。

所以我知道这适用于 ID。

<script>
$(document).ready(function(){
  $('#demo').text('The replaced text.');
});
</script>

所以我的问题是,如何将以下代码的文本(信用卡/借记卡(确认))更改为“确认”值。我无法使用 payment_method 名称,因为它与其他付款方式共享。

<label>
<input type="radio" name="payment_method" value="affirm">
Credit Card/Debit Card (Affirm)                            
</label>

提前谢谢你!

编辑 1:

整个 HTML 代码

<div id="content" class="col-xs-12 col-sm-12">
            
                <p>Please select your preferred payment method.</p>
            <div class="radio">
            <label>
                                                        <input type="radio" name="payment_method" value="authorizenet" checked="checked">
                                Credit Card/Debit Card (Authorize.Net)                            </label>
        </div>
            <div class="radio">
            <label>
                                    <input type="radio" name="payment_method" value="affirm">
                                Credit Card/Debit Card (Affirm)                            </label>
        </div>
    <p><strong>Add comments about your order.</strong></p>
<p>
    <textarea name="comment" rows="8" class="form-control"></textarea>
</p>
<div class="cart-module">
    <div class="cart-content">
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <h4 for="input-coupon">Coupon Code</h4>
                    <div class="input-group">
                        <input type="text" name="coupon" value="" placeholder="Coupon Code" id="input-coupon" class="form-control">
                        <span class="input-group-btn">
                            <input type="button" value="Apply" data-code="coupon" data-loading-text="Loading..." class="btn btn-primary">
                        </span>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <h4 for="input-voucher">Gift Certificate Code</h4>
                    <div class="input-group">
                        <input type="text" name="voucher" value="" placeholder="Gift Certificate Code" id="input-voucher" class="form-control">
                        <span class="input-group-btn">
                            <input type="button" value="Apply" data-code="voucher" data-loading-text="Loading..." class="btn btn-primary">
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
    <div class="buttons">
        <div class="pull-right">I have read and agree to the <a href="https://hhtruckaccessories.com?route=information%2Finformation%2Fagree&amp;information_id=5" class="agree"><b>Shipping and Returns</b></a>.                            <input type="checkbox" name="agree" value="1">
                        &nbsp;
            <input type="button" value="Continue" id="button-payment-method" data-loading-text="Loading..." class="btn btn-primary">
        </div>
    </div>
<script src="/js/catalog/checkout/payment_method/bundle.js?v=1.58.1" type="text/javascript"></script>        </div>

【问题讨论】:

  • 解释一下:您想知道如何更改指定单选按钮组中特定单选按钮的文本内容吗?
  • 是的,先生。我只想将文本从“信用卡/借记卡(确认)”简化为“确认”

标签: jquery text


【解决方案1】:

你不能使用 text() 输入,所以你必须重建 html 行

var newtext = "affirm";
selector = $("input[type=radio][value=affirm]").closest("label");
var line = selector.html().split(">")[0] + ">" + newtext;

selector.html(line);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="content" class="col-xs-12 col-sm-12">

  <p>Please select your preferred payment method.</p>
  <div class="radio">
    <label>
      <input type="radio" name="payment_method" value="authorizenet" checked="checked">
       Credit Card/Debit Card (Authorize.Net)  
    </label>
  </div>
  <div class="radio">
    <label>
      <input type="radio" name="payment_method" value="affirm">
        Credit Card/Debit Card (Affirm)
    </label>
  </div>

【讨论】:

  • 我收到一个错误,看起来像...```未捕获的类型错误:无法读取 HTMLDocument 处未定义的属性“拆分”。 (checkout?route=checkout%2Fcheckout:162)在 e (jquery.min.js:2) 在 t (jquery.min.js:2) ```
  • 你可以看到我没有错误..也许你应该显示你的 html 代码..你有更多的输入单选线吗?
  • 好的 - 我用编辑 1 编辑了我的原始帖子,显示更多代码。如果我能提供更多帮助,请告诉我。
  • 我没有错误。我刚刚添加了一个
    来关闭打开的div
  • 不,这不是jquery版本的问题,你能展示你完整的脚本js和html代码吗?在脚本的开头做一个 console.log($("input[type=radio][value=affirm]").closest("label").length) 并给我结果。
  • 猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 2012-02-16
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    相关资源
    最近更新 更多