1

The code is simply like this:

<select id="productextra[@count@]">
<option value="[@option_id@]">Yes</option>
<option value="[@option_id@]">No</option>
</select>

The button should be disable is: Add to cart

Pakalu Papito
  • 31
  • 1
  • 1
  • 8

4 Answers4

5

Write an onchange event for your select and find the selected value

<button id="btnSubmit" type="submit">Add to cart</button>

JS

$("select").on('change',function(){
   if($(this).find('option:selected').text()=="No")
       $("#btnSubmit").attr('disabled',true)
   else
       $("#btnSubmit").attr('disabled',false)
});

Sample Snippet

$("select").on('change',function(){
   if($(this).find('option:selected').text()=="No")
       $("#btnSubmit").attr('disabled',true)
   else
       $("#btnSubmit").attr('disabled',false)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="productextra[@count@]">
<option value="[@option_id@]">Yes</option>
<option value="[@option_id@]">No</option>
</select>
<button id="btnSubmit" type="submit">Add to cart</button>
Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
2
var sel = document.getElementById('productextra');
var sv = sel.options[sel.selectedIndex].value;

This will give the value of selected option in which you can disable the button using

document.getElementById("myBtn").disabled = true;
Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
osk
  • 71
  • 4
1

Your HTML should be like this:

<select id="productextra0">
<option value="0">Yes</option>
<option value="1">No</option>
</select>

<button type="submit" id="submit-button">Add to cart</button>

and javascript:

window.onload=function()
{
    document.getElementById("productextra0").onchange=function()
    {
        if(this.options[this.selectedIndex].value==1)
        {
            document.getElementById("submit-button").disabled=true;
        }
        else
        {
            document.getElementById("submit-button").disabled=false;
        }
    }
}

here is the working demo

I added an id for the add to cart button so it can easily be accessed via javascript document.getElementById, now I added event handler for dropdown onchange event, this means a function will trigger every time the dropdownchanges value. When dropdown change its value the function will check if its equal to 1 then it will disable your add to cart button, else it will enable it back.

I also put the function into windows.onload so that we can ensure that onchange function will only be attached to your dropdown when its ready or already been created by the browser.

VMcreator
  • 833
  • 8
  • 17
0

button

<button id="btn" type="submit">Add to cart</button>

jquery

$("#btn").prop("disabled", true);
Elyor
  • 5,396
  • 8
  • 48
  • 76