2017-10-13 95 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>