JS无法实现全选和反选 应该怎么改

JavaScript0101

JS无法实现全选和反选 应该怎么改,第1张

有几个地方可能有问题:

部分浏览器不支持form1.这种方法访问控件,使用document.方法。

if(elements[i].type=='checkbox') {

  if(elements[i].checked==false){

         elements[i].checked=true}}

这样写会比较好点:

if (elements[i].type == 'checkbox') elements[i].checked = !elements[i].checked

<!DOCTYtrE html trUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htttr://

<html xmlns="htttr://

<head>

<meta htttr-equiv="Content-Tytre" content="text/html charset=utf-8" />

<title>Example 1</title>

<script type="text/javascript">

function validate(form){

     var returnValue=true

     var name=form.fname.value

     if(name==""){

          returnValue=false

          alert("You must enter a name")

          document.example.txtname.focus()

      }

      var email=form.femail.value

      var rxemail= /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/

      if(!rxemail.test(email)){

           returnValue=false

           alert("You must enter a valid email address")

           document.example.femail.focus()

       }

      var radiochosen=false

      var radiobuttons=form.fanswer

      for(var i=0i<radiobuttons.lengthi++){

           if(radiobuttons[i].checked){

                radiochosen=true

           }

       }

      if(radiochosen==""){

           returnValue=false

           alert("You did not answer the question")

           document.example.fanswer.focus()

       }

           var tiebreak=form.textbreak.value

        if(tiebreak==""){

           returnValue=false

           alert("Youu must enter an answer for the tie breaker")

           document.example.textbreak.focus()

        }

       var tiebreaklength=textbreak.split(/\s+/g)

       wordcount=tiebreaklength.length

       if(wordcount>20){

            returnValue=false

            alert("Your tie breaker answer must be no more than 20 words. You entered "+wordcount+" words.")

            document.example.textbreak.focus()

        }

        return returnValue

 }

</script>

</head>

<body>

    <form name="example" method="post" action="competiotion.aspx" onSubmit="return validate(this)">

        <h1>An Example Competition Form</h1>

        <h1>(Sorry,there are no real prizes!)</h1>

        <p>To enter the drawing to win a case of Jenny's Jam,first answer this question"What color are strawberries?"Then provide an answer for the tie-breaker question "I would like to win a case of Jenny's Jam because..." in no more than 20 words.</p>

        <table align="center">

            <tr> 

                <td class="fname">Name:</td>

                <td><input type="text" id="fname" size="30" /></td>

            </tr>

            <tr>

                <td class="femail">Email:</td>

                <td><input type="text" id="femail"size="30" />

            </td>

            </tr>

            <tr>

                <td class="fanswer">Answer:</td>

                <td><input type="radio" value="red" name="fanswer"  />Red<br />

                <input type="radio" value="gray" name="fanswer"  />Gray<br />

                <input type="radio" value="blue" name="fanswer"  />Blue

                </td>

            </tr>

            <tr>

                <td class="fbreak">Tie breaker<br />(no more than 20 words):</td>

                <td><textarea rows="5" cols="30" name="textbreak" style="overflow-y:hidden"></textarea>

                </td>

            </tr>

            <tr>

                <td></td>

                <td><input type="submit" value="Enter now" /></td>

            </tr>

        </table>

    </form>

</body>

</html>

给你修改了一下,因为你的表单id和name没有跟js的对应上

比如 var name=form.fname.value这个是获取用户名

那么用户名的input就应该是<input type="text" id="fname" size="30" />(注意id)