jquery 如何使用Document.execCommand()更改下拉选项值

3qpi33ja  于 2023-01-16  发布在  jQuery
关注(0)|答案(1)|浏览(120)

我尝试在bigCommerce结帐页面上预填充值。由于结帐页面使用嵌入式react,这就是为什么我无法使用普通javascript方法更改字段值的原因。不过,我可以使用Document.execCommand()更改值。以下是更改名字的实际代码:

document.getElementById("firstNameInput").focus();
  document.execCommand("insertText", false, customerInfo.firstName);

但问题是我无法使用Document.execCommand()从下拉菜单中更改/选择国家。
所以我的问题是,如何使用document.execCommand更改下拉列表的值?

**PS:**我知道document.execCommand已经过时了,但是我现在没有任何替代方法来更改值,因为正如我所说,普通JavaScript方法不会更改bigcommerce结帐页面上的字段值。

jhkqcmku

jhkqcmku1#

I had the same issue, and found this solution:

var sel=document.querySelector('#id-of-your-selector');
sel.options[1].selected=true; //option no to be select here I am selection the option at index no# 1, you can change
var event = new Event('change', { bubbles: true }); //create his event
event.simulated=true; //add this too
sel.dispatchEvent(event);//dispatch event in this line sel is the element we created at first line for selector.

相关问题