覆盖困难的视图模型

for*_*tso 17 html javascript jquery shopify

我试图使用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>
Run Code Online (Sandbox Code Playgroud)

我运行这个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");
Run Code Online (Sandbox Code Playgroud)

我留下了以下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>
Run Code Online (Sandbox Code Playgroud)

问题是,每次单击输入字段时,该值都将恢复为页面加载时的值.

我也尝试在父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()
Run Code Online (Sandbox Code Playgroud)

父td功能:

new Shopify.EditVariantPrice(jQuery('#product_variants__price').parent())
Run Code Online (Sandbox Code Playgroud)

那么如何在输入中成功编辑此值并更新Shopify视图模型?

你可以去这里试试这个:

https://jebus333.myshopify.com/admin/products/2521183043

登录jebus333@mailinator.com密码shop1

编辑:我试图在页面上找到视图模型,但没有成功.另外,在编辑输入字段中的值时没有网络调用,这使我相信这些值正在从页面上的某个位置撤回.

bum*_*mpy 3

尝试这个:

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");
Run Code Online (Sandbox Code Playgroud)

如果它对您有用,那么以下内容可能就足够了:

Shopify.EditVariantPrice.prototype.onFocus = function(t) { 
  this.price = '50.00'; // Use the price you want here
};
Run Code Online (Sandbox Code Playgroud)