javascript - 无法读取未定义的属性“拆分”(调试问题)
问题描述
我很难弄清楚以下错误:
Uncaught TypeError: Cannot read property 'split' of undefined
的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&information_id=5" class="agree"><b>Shipping and Returns</b></a>. <input type="checkbox" name="agree" value="1">
<input type="button" value="Continue" id="button-payment-method" data-loading-text="Loading..." class="btn btn-primary">
</div>
</div>
剧本:
var newtext = "affirm";
selector = $("input[type=radio][value=affirm]").closest("label");
var line = selector.html().split(">")[0] + ">" + newtext;
selector.html(line);
目标:
我在结帐页面上有两个单选按钮。
- 信用卡/借记卡(Authorize.Net)
- 信用卡/借记卡(确认)
我无法直接编辑 HTML。因此用上面的代码重建 html 行,给我想要的输出。我正在尝试将第二个无线电输入的 HTML 文本更改为“确认”。
该脚本在小提琴中工作,但不在页面上。
解决方案
在尝试维护页面而不访问源文件时,我曾经遇到过类似的错误。我正在使用 jQuery 尝试删除 ap 元素语句,但由于它在应用程序甚至生成 html 的那部分之前就在主页上运行,所以出现了未定义的情况。这是一个SPA。我通过将文档上的侦听器添加到路由器来修复它,如下所示:$(document).on("pageshow", "#checkoutPage", function() {//your jquery function});
我在 CodePen 和本地使用 VS Code 都进行了尝试,两次都得到了预期的效果。您是否尝试过确保您使用的是正确版本的 JQuery,并且以正确的方式链接到 html,还通过控制台日志进行调试?见下文:
enter code here
推荐阅读
- clojure - 如何提取函数令牌生成器返回的值:(延迟(令牌生成器状态))
- linux - 如何使用 Rust 打开用户的默认编辑器并获取编辑后的内容?
- android - 形状的笔划不出现
- python - 即使在 VS Code 中运行,代码也没有在hackerrank 中运行
- javascript - 为什么我在尝试导入我的函数时收到“TypeError: Assignment to constant variable”?
- python - 从文本文件中读取数字
- mysql - 使用 mysql 计算最高和最低价格
- jenkins - 在 weblogic 集群上使用 Weblogic.deployer 部署 webapp
- reactjs - 如何在 React.js 中解析 JSON?
- javascript - 将不同列表中的两个可排序对象按其类移动到相同位置