簡體   English   中英

PouchDB:單選按鈕值未保存到數據庫

[英]PouchDB: radio button values not being saved to database

我有一個帶有單選按鈕的表單,該表單將數據保存到PouchDB實例。 文本字段將保存到PouchDB。

單選按鈕的值不會保存到PouchDB中 重新加載數據時,在輸入上單擊的值不會顯示。

(還添加了viewnote函數,以防它們無法正確顯示。)

的JavaScript

    PouchNotesObj.prototype.savenote = function () {
        'use strict';
        var o = {}, that = this;

        if (!this.formobject._id.value) {
            o._id = new Date().getTime() + '';
        } else {
            o._id = this.formobject._id.value;
        }

        if (this.formobject._rev.value) {
            o._rev = this.formobject._rev.value;
        }

        o.preferred      = (this.formobject.preferred.value == '') ? '' : this.formobject.preferred.value;
        o.application_access      = (this.formobject.application_access.value == '') ? '' : this.formobject.application_access.value;
        o.modified  = new Date().getTime();

        this.pdb.put(o, function (error, response) {
            if(error){
                that.showerror(error);
            }

            if(response && response.ok){

                that.viewnoteset();
                that.formobject.reset();

                that.show(that.formobject.dataset.show);
                that.hide(that.formobject.dataset.hide);

                viewnotes.dispatchEvent(new MouseEvent('click'));
            }
        });

        this.resethash();
    };

PouchNotesObj.prototype.viewnote = function (noteid) {
    'use strict';

    var that = this, noteform = this.formobject;

    this.pdb.get(noteid, {attachments:true}, function (error, response) {
        var fields = Object.keys(response), o, link, attachments, li;

        if (error) {
            this.showerror();
            return;
        } else {

            fields.map( function (f) {
                if (noteform[f] !== undefined && noteform[f].type != 'file') {
                    noteform[f].value = response[f];
                }


            // fill in form fields with response data.
            that.show('#addnote');
            that.hide('section:not(#addnote)');
            that.show('#attachments');
        }
    });

的CSS

.radio-container ul{
  list-style: none;
  width: 100%;
  margin: 0;
  padding: 0;
}

.radio-container ul li{
  display: block;
  position: relative;
}

.radio-container ul li input[type=radio]{
  position: absolute;
  visibility: hidden;
}

.radio-container ul li label{
  display: block;
  position: relative;
  font-weight: 400;
  padding: 5px 25px 5px 80px;
  margin: 10px auto;
  z-index: 9;
  cursor: pointer;
  -webkit-transition: all 0.25s linear;
}


.radio-container ul li .check {
  display: block;
  position: absolute;  border-radius: 100%;
  height: 25px;
  width: 25px;
  top: 5px;
  left: 20px;
    z-index: 5;
    transition: border .25s linear;
    -webkit-transition: border .25s linear;
}

ul li:hover .check {
  border: 5px solid #D10373;
}

.radio-container ul li .check::before {
  display: block;
  position: absolute;
    content: '';
  border-radius: 100%;
  height: 15px;
  width: 15px;
  top: 5px;
    left: 5px;
  margin: auto;
    transition: background 0.25s linear;
    -webkit-transition: background 0.25s linear;
}

的HTML

    <div>
        <label class="radiogroup-label text-label">Preferred means of contact:</label>
        <radiogroup>
        <div class="radio-container">
            <ul>
                <li><input type="radio" name="preferred" id="no_pref" value="No preference" checked="true"> <label class="radio-label" for="no_pref">No preference</label> <div class="check"></div></li>
                <li><input type="radio" name="preferred" id="by_mobile1" value="Mobile 1"> <label class="radio-label" for="by_mobile1">Mobile 1</label> <div class="check"></div></li>
                <li><input type="radio" name="preferred" id="by_mobile2" value="mobile 2"> <label class="radio-label" for="by_mobile2">Mobile 2</label> <div class="check"></div></li>
                <li><input type="radio" name="preferred" id="by_home_landline" value="Home landline"> <label class="radio-label" for="by_home_landline">Home landline</label> <div class="check"></div></li>
                <li><input type="radio" name="preferred" id="by_work_landline" value="Work landline"> <label class="radio-label" for="by_work_landline">Work landline</label> <div class="check"></div></li>
                <li><input type="radio" name="preferred" id="by_home_email" value="Home email"> <label class="radio-label" for="by_home_email">Home email</label> <div class="check"></div></li>
                <li><input type="radio" name="preferred" id="by_work_email" value="Work email"> <label class="radio-label" for="by_work_email">Work email</label> <div class="check"></div></li>
            </ul>
        </div>
        </radiogroup>
    </div>
    <p>&nbsp;</p>
    <div>
        <label class="radiogroup-label text-label">Can edit <br/>directory entries:</label>
        <radiogroup>
        <div class="radio-container">
            <ul>
                <li><input type="radio" name="application_access" id="is_admin" value="Admin"> <label class="radio-label" for="is_admin">Yes</label> <div class="check"></div></li>
                <li><input type="radio" name="application_access" id="is_user" value="User" checked="checked"> <label class="radio-label" for="is_user">No</label> <div class="check"></div></li>
            </ul>
        </div>
        </radiogroup>
    </div>

單選按鈕選擇的值沒有保存到數據庫中,因為JavaScript沒有得到它。

它需要指出已檢查input的值。

var selectedPref = document.querySelector('input[name="preferred"]:checked').value;
o.preferred      = (selectedPref == '') ? '' : selectedPref;
var selectedAppAccess = document.querySelector('input[name="application_access"]:checked').value;
o.application_access      = (selectedAppAccess == '') ? '' : selectedAppAccess;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM