Are there legitimate uses for JavaScript's “with” statement?

后端 未结 30 2329
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 04:22

Alan Storm\'s comments in response to my answer regarding the with statement got me thinking. I\'ve seldom found a reason to use this particular language feature, and had ne

30条回答
  •  逝去的感伤
    2020-11-22 05:15

    You got to see the validation of a form in javascript at W3schools http://www.w3schools.com/js/js_form_validation.asp where the object form is "scanned" through to find an input with name 'email'

    But i've modified it to get from ANY form all the fields validate as not empty, regardless of the name or quantity of field in a form. Well i've tested only text-fields.

    But the with() made things simpler. Here's the code:

    function validate_required(field)
    {
    with (field)
      {
      if (value==null||value=="")
        {
        alert('All fields are mandtory');return false;
        }
      else
        {
        return true;
        }
      }
    }
    
    function validate_form(thisform)
    {
    with (thisform)
      {
        for(fiie in elements){
            if (validate_required(elements[fiie])==false){
                elements[fiie].focus();
                elements[fiie].style.border='1px solid red';
                return false;
            } else {elements[fiie].style.border='1px solid #7F9DB9';}
        }
    
      }
      return false;
    }
    

提交回复
热议问题