2014-10-27 62 views
0

嗨everyboby我只是想嘗試驗證一些輸入和textareas與JavaScript使用開關,但它似乎不能正常工作,我的功能validarorden看起來像這樣。切換案例與javascript

function validarorden(){ 
    var validado = true; 

    var tipo = document.getElementById("cmbTipoEquipo").value; //select 
    var marca = document.getElementById("cmbMarcaEquipo").value; //select 
    var modelo = document.getElementById("txtModeloEquipo").value; //input 
    var nserie = document.getElementById("txtNSerieEquipo").value; //input 
    var descripcion = document.getElementById("txtaReparacion").value; //textarea 

    //next alert shows '0 - 0 - - -' when I click on the button for the fisrt time 
    alert(tipo+" - "+marca+" - "+modelo+" - "+nserie+" - "+descripcion); 

    switch(tipo) { 
     case 0: 
      alert("debes elegir el tipo"); 
      validado = false; 
     break; 
    } 

    switch(marca) { 
     case 0: 
      alert("debes elegir la marca"); 
      validado = false; 
     break; 
    } 

    switch(modelo.length) { 
     case 0: 
      alert("debes ingresar el modelo"); 
      validado = false; 
     break; 
    } 

    switch(nserie.length) { 
     case 0: 
      alert("debes ingresar el numero de serie"); 
      validado = false; 
     break; 
    } 

    switch(descripcion) { 
     case (descripcion.length == 0): 
      alert("debes ingresar la descripcion"); 
      validado = false; 
     break; 
     case (descripcion.length > 200): 
      alert("la descripcion no debe ser mayor a 200"); 
      validado = false; 
     break; 
     case (descripcion.length < 50): 
      alert("la descripcion debe ser de al menos 50"); 
      validado = false; 
     break; 

    } 

    return validado; 
} 

我用jquery事件調用這個函數。

$("body").on("click","#btnGenerarOrden",function(){ 
     if(validarorden()) { 
      alert("Correcto"); 
     } 
    }); 

莫德洛nserie變量的情況下工作正常,但不是他們的休息,HTML代碼是下一個。

<div class="col-md-4"> 
    <div class="form-group fltipo"> 
    <label class="control-label" for="cmbTipoEquipo">Tipo</label> 
    <select id="cmbTipoEquipo" class="form-control"> 
    <option value="0">Tipo</option> 
    <option value="1">Latop</option> 
    </select> 
    </div> 
</div> 

    <div class="col-md-4"> 
     <div class="form-group flmarca"> 
      <label class="control-label" for="cmbMarcaEquipo">Marca</label> 
      <select id="cmbMarcaEquipo" class="form-control"> 
      <option value="0">marca</option> 
      <option value="1">HP</option> 
      </select> 
     </div>       
    </div> 
    <div class="col-md-4"> 
     <div class="form-group flmodelo"> 
      <label class="control-label" for="txtModeloEquipo">Modelo</label> 
      <input type="text" id="txtModeloEquipo" class="form-control"> 
     </div> 
    </div> 
    <div class="col-md-4"> 
     <label class="control-label" for="txtNSerieEquipo">N.Serie</label> 
     <div class="input-group flnserie"> 
     <input type="text" id="txtNSerieEquipo" class="form-control"> 
     <span class="input-group-btn"> 
      <button class="btn btn-default" id="btnAutoNS" type="button"><span class="glyphicon 
glyphicon-random"></span></button> 
     </span> 
    </div> 
    </div> 
    <div class="col-md-8"> 
     <div class="form-group fldescr"> 
      <label class="control-label" for="txtaReparacion">Descripción de la reparación</label> 
      <textarea class="form-control" id="txtaReparacion" cols="30" rows="3"></textarea> 
     </div> 
     </div> 

我希望你們能幫助我

+2

我建議if語句switch語句在這裏 – 2014-10-27 00:25:21

+0

把雙引號圍繞case 0 ...所以case:「0」 – jroot 2014-10-27 00:30:05

回答

0

你要在這裏體會到什麼是一個開關case語句使用===比較。這意味着被比較變量的類型必須相同。因此,將xxxxx.length0進行比較是可以的,因爲長度返回整數。

但是比較來選擇元素的值返回一個字符串,所以你需要把各地報價0像這樣 case "0":

你也可以使用switch (parseInt(xxx))來解決該問題

0

在任何命令式編程語言其中包括switch功能,在case語句必須是常量,生成常量(如getter)或字符串文字的表達式之後測試的常量。

你的主要問題是,你嘗試應用switch語句的布爾:

switch(descripcion) { 
    case (descripcion.length == 0): 
     alert("debes ingresar la descripcion"); 
     validado = false; 
    break; 
    case (descripcion.length > 200): 
     alert("la descripcion no debe ser mayor a 200"); 
     validado = false; 
    break; 
    case (descripcion.length < 50): 
     alert("la descripcion debe ser de al menos 50"); 
     validado = false; 
    break; 
} 

在代碼中的這一部分,你嘗試測試真或假的陳述。你應該寫一個if/else if塊來測試長度值。

使用switch語句這種方式會像在問,

是在文本框中等於長度大於200的信息?

它只是沒有任何意義。

您遇到的另一個問題是將字符串與常量進行比較。輸入場的value屬性爲String類型,這意味着你需要得到的field.value長度比較它恆定的,就像這樣:

switch (tipo.length) { 
    case 0: 
     ... 
    break; 
} 

參考http://en.wikipedia.org/wiki/JavaScript_syntax#Switch_statement爲規則的完整列表切換語句。