jquery - Magento2 - 添加一个在升级前运行良好的小 jquery
问题描述
我正在使用的代码是这样的:
jQuery(document).ready(function($){
if(window.location.href =='https://mysite/csproduct/vproducts/new/set/4/type/simple/'){
console.log('ready');
$("[name='product[vendor_price]']").keyup(function() {
$("[name='product[price]']").val($(this).val()).change();
});
}
});
如果我将代码粘贴到控制台中,它可以工作。即使没有粘贴我正在准备的代码
console.log
- 准备好了。除此之外,控制台中没有错误。
这里使用的 HTML 是这样的:
<div class="admin__field _required" data-bind="css: $data.additionalClasses, attr: {'data-index': index}, visible: visible" data-index="vendor_price">
<label class="admin__field-label" data-bind="attr: {for: uid}, visible: $data.labelVisible" for="CTK9J41">
<span data-bind="attr: {'data-config-scope': $data.scopeLabel}, text: label" data-config-scope="[STORE VIEW]">Vendor Price</span>
</label>
<div class="admin__field-control" data-bind="css: {'_with-tooltip': $data.tooltip, '_with-reset': $data.showFallbackReset && $data.isDifferedFromDefault}">
<input class="admin__control-text" type="text" data-bind="
event: {change: userChanges},
value: value,
hasFocus: focused,
valueUpdate: valueUpdate,
attr: {
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid,
disabled: disabled
}" name="product[vendor_price]" aria-describedby="notice-CTK9J41" id="CTK9J41">
</div>
</div>
<div class="admin__field admin__field-small _required" data-bind="css: $data.additionalClasses, attr: {'data-index': index}, visible: visible" data-index="price">
<span data-bind="attr: {'data-config-scope': $data.scopeLabel}, text: label" data-config-scope="[STORE VIEW]">Price</span>
</label>
<div class="admin__field-control" data-bind="css: {'_with-tooltip': $data.tooltip, '_with-reset': $data.showFallbackReset && $data.isDifferedFromDefault}">
<div class="admin__control-addon">
<input class="admin__control-text" type="text" data-bind="
event: {change: userChanges},
value: value,
hasFocus: focused,
valueUpdate: valueUpdate,
attr: {
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid,
disabled: disabled
}" name="product[price]" aria-describedby="notice-JHLHU2O" id="JHLHU2O">
<label class="admin__addon-prefix" data-bind="attr: {for: uid}" for="JHLHU2O">
<span data-bind="text: addbefore">₹</span>
</label>
</div>
</div>
解决方案
Magento 2 使用 Knockout.js 框架来动态构建其前端的某些部分。这个构建也将在 jQueryready
事件触发后发生。因此,name='product[vendor_price]'
当您查找它时,可能在 DOM 中尚不可用该元素。
$(document).on("keyup", "[name='product[vendor_price]']", function() {
$("[name='product[price]']").val($(this).val()).change();
});
注意:与您的问题无关,但尝试使用“input”事件而不是“keyup”:这也将响应通过拖放、上下文菜单(清除、剪切、粘贴)或其他输入所做的更改设备,或者当一个键被按下并重复时。
推荐阅读
- python - Python Pandas - 将列表转换为系列
- c++ - 在 C/C++ 中计算一维信号的互相关
- java - 如何在支持多种屏幕尺寸的android中截屏后立即添加图标或水印图像
- python - 如何通过仅获取一些键从另一个字典列表创建字典列表:python
- javascript - 单击按钮时从对象数组中删除一个条目
- php - Laravel 外观 Slug 不适用于非英语?
- macos - 无法在 Ansible 中运行 CURL 命令
- mysql - 如何在gorm模型中传递动态表名
- unity3d - 使用后置摄像头进行 AR,同时使用前置摄像头捕获输入
- python - 正确识别字符串中的表情符号(Python)