简体   繁体   中英

Chrome Extension to submit a form automatically

I'm new to building chrome extension and I'm trying to create one that will automatically select a size of an item and then submit the form. I have the size selection working correctly, but it's not submitting the form. I tried to use the click() function to click the button, but it's not working either.

Below is the button html coding from the form:

<form action="http://*************HRtbA,,/product/102374/" method="post" id="product_addtocart_form">
<button type="button" data-track-event="Product Page - Add to Cart" title="Add to Cart" class="button btn-cart" onclick="productAddToCartForm.submit(this)">`

Here is my code:

function fRun()
{
    // Select size option.
    var sizesList=document.getElementsByName("super_attribute[138]")[0];
    for(var i=0; i<sizesList.length; i++)
    {
        if(sizesList.options[i].text.trim() == size_i_want)
        {
            sizesList.selectedIndex = i;
            var input = document.getElementsByClassName("button btn-cart");
            input.click();
        }
    }

Any help is appreciated.

getElementsByClassName返回一个元素数组,而不是单个元素,因此您将需要对输入变量进行索引以获得所需的实际元素。

input[0].click();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM