admin管理员组

文章数量:1403240

I have written the below code. But it does not show me the alert message when I don't select the other value and click onto the submit button. I don't want to use getElementbyId. I am using the name attribute of the HTML.

<HTML>
<HEAD>
<TITLE>ComboBox Validation</TITLE>

   <script Language="JavaScript">

   function validate()
   {

      if (documentboForm.technology.value=="0") \
     {
         alert("Please Select Technology");
     }

   }
</script>
</HEAD>

<BODY>

<form name="boForm">
<select name="technology">
    <option value="0">Select</option>
    <option value="1">Java Server Pages</option>
</select>

  <input type="submit" value="submit" onClick="validate();">
</form>

</BODY>
</HTML>

I have written the below code. But it does not show me the alert message when I don't select the other value and click onto the submit button. I don't want to use getElementbyId. I am using the name attribute of the HTML.

<HTML>
<HEAD>
<TITLE>ComboBox Validation</TITLE>

   <script Language="JavaScript">

   function validate()
   {

      if (document.boForm.technology.value=="0") \
     {
         alert("Please Select Technology");
     }

   }
</script>
</HEAD>

<BODY>

<form name="boForm">
<select name="technology">
    <option value="0">Select</option>
    <option value="1">Java Server Pages</option>
</select>

  <input type="submit" value="submit" onClick="validate();">
</form>

</BODY>
</HTML>
Share Improve this question edited Jan 23, 2012 at 17:21 Alex Turpin 47.8k23 gold badges116 silver badges146 bronze badges asked Jan 23, 2012 at 17:17 user460920user460920 8875 gold badges17 silver badges27 bronze badges 3
  • That isn't a bobox. It is a "Drop down menu". A bobox is a bination of a drop down menu and a text input (hence the name). – Quentin Commented Jan 23, 2012 at 17:19
  • 1 Why don't you want to use IDs? That's the safest, most reliable, fastest way. That owuld be like writing HTML tags in all caps. Oh wait... – Alex Turpin Commented Jan 23, 2012 at 17:21
  • ok..so how to validate the bobox then ?? – user460920 Commented Jan 23, 2012 at 17:30
Add a ment  | 

1 Answer 1

Reset to default 3

I think you want:

if (document.forms["boForm"].technology.value == "0")

But really, stop avoiding document.getElementById. That's the clearest, easiest way to deal with this:

<select id="ddTechnology" name="technology">
    <option value="0">Select</option>
    <option value="1">Java Server Pages</option>
</select>

if (document.getElementById("ddTechnology").value == "0")

本文标签: validate combobox in javascriptStack Overflow