2017-09-06 50 views
0

我有一個HTML表單,我需要將插入的數據保存在mongo數據庫中。我遇到的困難是保存單選按鈕和ckeckboxes插入的數據。如何使用單選按鈕和複選框在mongodb上保存HTML表單中的數據

這裏是我的HTML代碼:

<div class="form-group"> 
    <label>Foto:</label> 
    <input class="form-control" type="text" name="animal[foto]" placeholder="image url"> 
    </div> 
    <div class="form-group"> 
    <label>Animal:</label> 
    <div class="form-check"> 
     <label class="form-check-label"><input class="form-check-input" name="animal[tipo]" type="radio" value="dog" > Dog</label> 
    </div> 
    <div class="form-check"> 
     <label class="form-check-label"><input class="form-check-input" name="animal[tipo]" type="radio" value="cat"> Cat</label> 
    </div> 
    <div class="form-check"> 
     <label class="form-check-label"><input class="form-check-input" name="animal[tipo]" type="radio" value="other"> Other:</label> 
     <input type="text" class="form-control" name="animal[tipo]" placeholder="Which?"> 
    </div> 
    </div> 
    <div class="form-group"> 
    <label>Gender:</label> 
    <div class="form-check"> 
     <label class="form-check-label"><input class="form-check-input" name="animal[sexo]" type="radio" value="Male"> Male</label> 
    </div> 
    <div class="form-check"> 
     <label class="form-check-label"><input class="form-check-input" name="animal[sexo]" type="radio" value="Female"> Female</label> 
    </div> 

我如何保存什麼是無線電BTN選擇?

回答

0

首先,您必須從單選按鈕中獲取值。 如果您使用的是javascript庫像jQuery,這是非常容易的:

alert($('input[name=animal[tipo]]:checked').val()); 

該代碼將選擇animal[tipo]名檢查輸入,並得到它的價值。簡單不是嗎?

然後您可以像往常一樣保存它

相關問題