我试图使用JS替换输入字段中的一些文本,但视图模型每次都会覆盖我的命令。这是我开始使用的HTML:
<td class="new-variants-table__cell" define="{ editVariantPrice: new Shopify.EditVariantPrice(this) }" context="editVariantPrice" style="height: auto;">
<input type="hidden" name="product[variants][][price]" id="product_variants__price" value="25.00" bind="price" data-dirty-trigger="true">
<input class="mock-edit-on-hover tr js-no-dirty js-variant-price variant-table-input--numeric" bind-event-focus="onFocus(this)" bind-event-blur="onBlur(this)" bind-event-input="onInput(this)">
</td>
我运行这个JS:
jQuery('#product_variants__price').siblings().removeAttr('bind-event-focus');
jQuery('#product_variants__price').siblings().removeAttr('bind-event-input');
jQuery('#product_variants__price').siblings().removeAttr('bind-event-blur');
jQuery('#product_variants__price').siblings().focus()
jQuery('#product_variants__price').siblings().val("34.00");
jQuery('#product_variants__price').val("34.00");
剩下的HTML代码如下:
<td class="new-variants-table__cell" define="{ editVariantPrice: new Shopify.EditVariantPrice(this) }" context="editVariantPrice" style="height: auto;">
<input type="hidden" name="product[variants][][price]" id="product_variants__price" value="34.00" bind="price" data-dirty-trigger="true">
<input class="mock-edit-on-hover tr js-no-dirty js-variant-price variant-table-input--numeric">
</td>
问题是,每次单击输入字段时,值都会恢复为页面加载时的值。
我还尝试在父td中运行该命令,同时更改我的值,以模拟对变量的编辑,并防止默认没有成功:
jQuery('#product_variants__price').siblings().bind('input', function (e) {
e.preventDefault();
return false;
});
jQuery('#product_variants__price').siblings().bind('focus', function (e) {
e.preventDefault();
return false;
});
jQuery('#product_variants__price').siblings().focus()
jQuery('#product_variants__price').siblings().val("£34.00");
jQuery('#product_variants__price').val("£34.00");
jQuery('#product_variants__price').siblings().keydown()
父td函数:
new Shopify.EditVariantPrice(jQuery('#product_variants__price').parent())
那么,我如何才能成功地编辑输入中的此值并更新视图模型呢?
您可以通过转到此处来亲自尝试:
登录jebus333@mailinator.com密码shop1
编辑:我试图在页面上找到视图模型,但没有成功。此外,在编辑输入字段中的值时没有网络调用,这让我相信这些值是从页面上的某个位置拉回的。
发布于 2015-09-10 03:56:16
试试这个:
var old = Shopify.EditVariantPrice.prototype.onFocus;
Shopify.EditVariantPrice.prototype.onFocus = function(t) {
this.price = '50.00'; // Use the price you want here
old.call(this, t);
};
jQuery('#product_variants__price').siblings().triggerHandler("focus");
jQuery('#product_variants__price').siblings().triggerHandler("blur");
如果它对您有效,那么以下内容可能就足够了:
Shopify.EditVariantPrice.prototype.onFocus = function(t) {
this.price = '50.00'; // Use the price you want here
};
发布于 2015-09-03 21:48:14
好吧,有一种肮脏的解决方案...
首先,你需要一个sendkeys插件。实际上,这意味着您需要包含this和this JS库(只需在控制台中复制粘贴它们即可进行测试)。如果你不想使用第一个库(我个人觉得对于这么小的东西来说它相当大),你可以只从中提取关键的东西,并且只使用它们。
下一步是创建一个函数,它的行为将像一个真正的用户:
function input(field, desiredValue) {
// get the currency symbol while value is still pristine
var currency = field.val()[0];
// move focus to the input
field.click().focus();
// remove all symbols from the input. I took 10, but of course you can use value.length instead
for (var i = 0; i < 10; i++) field.sendkeys("{backspace}");
// send the currency key
field.sendkeys(currency);
// send the desired value symbol-by-symbol
for (var i = 0; i < desiredValue.length; i++) field.sendkeys(desiredValue[i]);
}
然后,您可以简单地使用您希望赋值的值来调用它:
input($("#product_variants__price").next(), "123.00");
由于缺乏时间,我没有成功地伪造模糊事件;这就是为什么我被迫读取货币并将.00
作为字符串传递的原因。无论如何,你已经有了一条路要走,并且已经有了一个相当有效的解决方案。
发布于 2015-09-10 19:48:02
看起来你正在尝试在Shopify的管理面板中自动编辑产品的不同价格。
我建议使用Shopify的批量产品编辑器,而不是玩弄Shopify的管理页面的DOM,它可以让你在一个屏幕上设置所有变体的价格。我觉得在批量产品编辑器页面上使用JavaScript设置不同的价格会更好。
点击“编辑产品”按钮,如下图所示,将打开批量产品编辑器。
还要检查基于浏览器的宏录制插件(如iMacro )是否能为您提供帮助(您也可以在iMacro中使用JS编写宏代码)。
https://stackoverflow.com/questions/32252575
复制相似问题