admin管理员组

文章数量:1022834

How do you write the if statement for a select option using javascript?

I wrote a form and made a select option list. I want to write an if statement that will do something if an option is selected.

JS

function name(obj) {
    if (option.value == "cat") {
        document.getElementById("owner").jim.disabled = true;
    }
}

HTML

<p>
    selection:*
    <select id="Sone" name="Selection" size="1">
        <option>Select</option>
        <option value="cat" onclick="name(this);">cat</option>
        <option value="dog" onclick="name(this);">dog</option>
        <option value="bird" onclick="name(this);">bird</option>
        <option value="Other" onclick="name(this);">Other</option>
    </select>
</p>

How do you write the if statement for a select option using javascript?

I wrote a form and made a select option list. I want to write an if statement that will do something if an option is selected.

JS

function name(obj) {
    if (option.value == "cat") {
        document.getElementById("owner").jim.disabled = true;
    }
}

HTML

<p>
    selection:*
    <select id="Sone" name="Selection" size="1">
        <option>Select</option>
        <option value="cat" onclick="name(this);">cat</option>
        <option value="dog" onclick="name(this);">dog</option>
        <option value="bird" onclick="name(this);">bird</option>
        <option value="Other" onclick="name(this);">Other</option>
    </select>
</p>
Share Improve this question edited Apr 12, 2014 at 7:42 Nicolae Olariu 2,5552 gold badges19 silver badges31 bronze badges asked Apr 12, 2014 at 7:30 user3500239user3500239 311 gold badge1 silver badge6 bronze badges 2
  • function name(obj) { if (option.value == "cat") { document.getElementById("owner").jim.disabled=true ; } } – user3500239 Commented Apr 12, 2014 at 7:33
  • <p>selection:* <select id= "Sone" name="Selection" size="1"> <option>Select</option> <option value="cat" onclick="name(this);">cat</option> <option value="dog" onclick="name(this);">dog</option> <option value="bird" onclick="name(this);">bird</option> <option value="Other" onclick="name(this);">Other</option> </select></p> – user3500239 Commented Apr 12, 2014 at 7:33
Add a ment  | 

2 Answers 2

Reset to default 1
<select id= "Sone" name="Selection" onchange="check()">
<option value="cat">cat</option> 
<option value="dog">dog</option>
</select>
<input type="text" id="res" size="25"/>

And the function is :-

function check() {
 var val = document.getElementById('Sone').value;
 if(val=='cat') {
  document.getElementById('res').value = "Thanks for selecting cat";
 } else {
  document.getElementById('res').value = "please select CAT";
 }
}

You have to work as below

   <form name="AddEditUser">
   .
   .
   <select name="listCompany"  style="width:200px"  id="listCompany" onchange="fnCompany()">
   <option value="None">None</option>
   <option value="ABC">ABC</option>
   <option value="XYZ">XYZ</option>
   </select>

   </form>

You have to call the javascript function a

 function fnCompany(){

   if(document.frmAddEditUser.listCompany.value=='None'){
      //do something
   }
 }

How do you write the if statement for a select option using javascript?

I wrote a form and made a select option list. I want to write an if statement that will do something if an option is selected.

JS

function name(obj) {
    if (option.value == "cat") {
        document.getElementById("owner").jim.disabled = true;
    }
}

HTML

<p>
    selection:*
    <select id="Sone" name="Selection" size="1">
        <option>Select</option>
        <option value="cat" onclick="name(this);">cat</option>
        <option value="dog" onclick="name(this);">dog</option>
        <option value="bird" onclick="name(this);">bird</option>
        <option value="Other" onclick="name(this);">Other</option>
    </select>
</p>

How do you write the if statement for a select option using javascript?

I wrote a form and made a select option list. I want to write an if statement that will do something if an option is selected.

JS

function name(obj) {
    if (option.value == "cat") {
        document.getElementById("owner").jim.disabled = true;
    }
}

HTML

<p>
    selection:*
    <select id="Sone" name="Selection" size="1">
        <option>Select</option>
        <option value="cat" onclick="name(this);">cat</option>
        <option value="dog" onclick="name(this);">dog</option>
        <option value="bird" onclick="name(this);">bird</option>
        <option value="Other" onclick="name(this);">Other</option>
    </select>
</p>
Share Improve this question edited Apr 12, 2014 at 7:42 Nicolae Olariu 2,5552 gold badges19 silver badges31 bronze badges asked Apr 12, 2014 at 7:30 user3500239user3500239 311 gold badge1 silver badge6 bronze badges 2
  • function name(obj) { if (option.value == "cat") { document.getElementById("owner").jim.disabled=true ; } } – user3500239 Commented Apr 12, 2014 at 7:33
  • <p>selection:* <select id= "Sone" name="Selection" size="1"> <option>Select</option> <option value="cat" onclick="name(this);">cat</option> <option value="dog" onclick="name(this);">dog</option> <option value="bird" onclick="name(this);">bird</option> <option value="Other" onclick="name(this);">Other</option> </select></p> – user3500239 Commented Apr 12, 2014 at 7:33
Add a ment  | 

2 Answers 2

Reset to default 1
<select id= "Sone" name="Selection" onchange="check()">
<option value="cat">cat</option> 
<option value="dog">dog</option>
</select>
<input type="text" id="res" size="25"/>

And the function is :-

function check() {
 var val = document.getElementById('Sone').value;
 if(val=='cat') {
  document.getElementById('res').value = "Thanks for selecting cat";
 } else {
  document.getElementById('res').value = "please select CAT";
 }
}

You have to work as below

   <form name="AddEditUser">
   .
   .
   <select name="listCompany"  style="width:200px"  id="listCompany" onchange="fnCompany()">
   <option value="None">None</option>
   <option value="ABC">ABC</option>
   <option value="XYZ">XYZ</option>
   </select>

   </form>

You have to call the javascript function a

 function fnCompany(){

   if(document.frmAddEditUser.listCompany.value=='None'){
      //do something
   }
 }

本文标签: javascripthow would you write the if() statement for selection options in a formStack Overflow