2017-10-13 87 views
-2

这里我有一个表单,一个表单域是复选框,假设我点击病房长袍&灯光并点击提交按钮表示我正在使用这个价值和推到我的JSON格式到现在它工作正常,我的问题是我必须变量假设在该变量!空意味着我想推入这个值到我的json.one更多的条件,我们必须检查,就像我想在形式我点击数值是病房长袍&冰箱和我的var needtopushval ='冰箱'; Fride是两个时间是如此不应该这样varaible推到我的JSON如何将检查值和变量值推送到json数组

function rentfunction(){ 
 
var needtopushval=' Lights'; 
 
    var arr1 = []; 
 
    var data={ 
 
      "rentProperty": { 
 
          "fullName" : "Some Name" 
 
          }, 
 
      "floorType":[] 
 
     }; 
 
       $.each($("input[name='furniture_check']:checked"),function(){ 
 
       var furniture = $(this).val(); 
 
       arr1.push({"floorTypeName": furniture }); 
 
       }); 
 
    arr1.push({"floorTypeName": needtopushval }); 
 

 
      data.floorType=arr1; 
 
      //or data["floorType"]=arr1; 
 

 
       console.log(data); 
 
      }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
<form> 
 
\t <input type="checkbox" name="furniture_check" value="Ward robe">Ward robe <br> 
 
\t <input type="checkbox" name="furniture_check" value="Lights">Lights <br> 
 
\t <input type="checkbox" name="furniture_check" value="Fan">Fan <br><br><br> 
 
    
 
    <button type="button" id="rentBtnSubmit" onclick="rentfunction()">Submit</button> 
 
</form>

我的预期JSON

{ 
    "rentProperty": { 
    "fullName": "Some Name" 
    }, 
    "floorType": [ 
    { 
     "floorTypeName": "Ward robe" 
    }, 
    { 
     "floorTypeName": "Lights" 
    }, 
    { 
     "floorTypeName": "Fridge" 
    } 
    ] 
} 
+0

您可以使用这个'data.floorType.push({__YOUR_OBJECT __})' –

+0

Rohit将对象直接推送到'floorType',因为您的问题*我的问题*不清楚。你可以换个不同的方式,也许在两个不同的段落中有例子输入/输出吗? – gurvinder372

+0

@SK Jajoriya,是的正确我实施检查我的代码片段,主要问题是在表单字段我正在检查灯光和我的needtopushval也灯光所以两个时间相同的价值推动对象,但对我来说只需要一次只推值 –

回答

0

我希望它会帮助你解决您的问题

function rentfunction(){ 
 
    var needtopushval='Lights'; 
 
    var arr1 = []; 
 
    var data={ 
 
      "rentProperty": { 
 
          "fullName" : "Some Name" 
 
          }, 
 
      "floorType":[] 
 
     }; 
 
    // 
 
    $.each($("input[name='furniture_check']:checked"),function(e)  { 
 
     var furniture = $.trim($(this).val()); 
 
     var index=data.floorType.indexOf({"floorTypeName": furniture }); 
 
     if(index==-1){ 
 
       data.floorType.push({"floorTypeName": furniture }); 
 
      } 
 
     }); 
 
    var flag=0; 
 
    $.each(data.floorType,function(e){    
 
     if($.trim(data.floorType[e].floorTypeName) === $.trim(needtopushval)){ 
 
      flag=1; 
 
     } 
 
    }); 
 
    if(flag==0){ 
 
     data.floorType.push({"floorTypeName": $.trim(needtopushval)}); 
 
    } 
 
    
 
    console.log(data); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
    <form> 
 
    \t <input type="checkbox" name="furniture_check" value="Ward robe">Ward robe <br> 
 
    \t <input type="checkbox" name="furniture_check" value="Lights">Lights <br> 
 
    \t <input type="checkbox" name="furniture_check" value="Fan">Fan <br><br><br> 
 
     
 
     <button type="button" id="rentBtnSubmit" onclick="rentfunction()">Submit</button> 
 
    </form>

+0

不正确,因为假设我选择Ward robe&Fan意味着我们必须推var var needtopushval ='Lights';答案应该来这样 { “rentProperty”:{ “全名”: “有些名称” }, “floorType”: { “floorTypeName”: “沃德袍” }, { “floorTypeName “: “清香” },{ “floorTypeName”: “范” } ] } –

+0

@RohitM我已经更新了我的答案 –

0

希望下面的代码可以帮助

代码来检查非空值和天气的价值已经存在

var pos = arr1.map(function(e) { 
    return e.floorTypeName; 
}).indexOf(furniture); 

if (pos == -1 && needtopushval) { 
    arr1.push({ 
     "floorTypeName": needtopushval 
    }); 
} 

function rentfunction() { 
 
    var needtopushval = ' Lights'; 
 
    var arr1 = []; 
 
    var data = { 
 
     "rentProperty": { 
 
      "fullName": "Some Name" 
 
     }, 
 
     "floorType": [] 
 
    }; 
 
    $.each($("input[name='furniture_check']:checked"), function() { 
 
     var furniture = $(this).val(); 
 
     if (furniture) { 
 
      arr1.push({ 
 
       "floorTypeName": furniture 
 
      }); 
 
     } 
 

 
     //Code to Check weather a value exist and notempty 
 
     var pos = arr1.map(function(e) { 
 
      return e.floorTypeName; 
 
     }).indexOf(furniture); 
 

 

 
     if (pos == -1 && needtopushval) { 
 
      arr1.push({ 
 
       "floorTypeName": needtopushval 
 
      }); 
 
     } 
 

 
    }); 
 

 
    data.floorType = arr1; 
 
    //or data["floorType"]=arr1; 
 

 
    console.log(data); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
<form> 
 
\t <input type="checkbox" name="furniture_check" value="Ward robe">Ward robe <br> 
 
\t <input type="checkbox" name="furniture_check" value="Lights">Lights <br> 
 
\t <input type="checkbox" name="furniture_check" value="Fan">Fan <br><br><br> 
 
    
 
    <button type="button" id="rentBtnSubmit" onclick="rentfunction()">Submit</button> 
 
</form>