如果选择单选按钮,则添加动态文本框+警报错误

时间:2017-03-30 13:49:27

标签: javascript html indexeddb

我遇到两个无法解决的问题:

  1. 如果所有内容都已正确保存,代码应显示警报。我发出了警告信息,但是如果我添加10个元素并单击“保存”,我会收到10个警报。

  2. 按下下拉列表中的单选按钮后,无法添加动态文本字段。我做了很多事情,但每当我植入它时,indexedDB都会停止工作。

  3. 奖金问题,如果有人知道答案: 3.如何保存我在“表单”选项卡中创建的表单,以便稍后搜索它们并将新信息放入?

    var db;
    function indexedDBOk() {
        return "indexedDB" in window;
    }
    document.addEventListener("DOMContentLoaded", function() {
        //No support? Go in the corner and pout.
        if(!indexedDBOk) return;
        var openRequest = indexedDB.open("idarticle_people4",1);
        openRequest.onupgradeneeded = function(e) {
            var thisDB = e.target.result;
            if(!thisDB.objectStoreNames.contains("people")) {
                thisDB.createObjectStore("people", {autoIncrement:true});
            }
        };
        openRequest.onsuccess = function(e) {
            db = e.target.result;
            //Listen for add clicks
            document.querySelector("#addButton").addEventListener("click", addPerson, false);
    
            openRequest.onsuccess = function(e) {
                alert('success: version 1 opened');
            };
    
            //Listen for get clicks
            document.querySelector("#getButton").addEventListener("click", getPerson, false);
        };
        openRequest.onerror = function(e) {
        }
    },false);
    
    function addPerson(e) {
        //console.log(i);
        var person= new Array(polje2);
        for (n=0; n<i; n++) {
            person.length=i;
            var element='#element';
            element+=n;
            var unos='#unos';
            unos+=n;
            var provjera = document.querySelector(element).type;
            var polje1 = document.querySelector(unos).innerHTML;
            if (provjera=='checkbox') {
                var polje2 = document.querySelector(element).checked
                }
            else {
                polje2 = document.querySelector(element).value
                 }
        if (polje2==='') {
            alert ('Uspješno snimljeno');
        }
            //default for OS list is all, default for type is read
        var transaction = db.transaction(["people"],"readwrite");
        //Ask for the objectStore
        var store = transaction.objectStore("people");
        person[n]=
        {pitanje: polje1, odgovor:polje2 };
    }
     //Perform the add
        var request = store.add(person);
        request.onerror = function(e) {
            console.log("Error",e.target.error.
                name);
            //some type of error handler
        };
        request.onsuccess = function(e) {
            console.log("Jej, uspjelo je");
        }
    }
    function getPerson(e) {
        var key = document.querySelector("#key").value;
        if(key === "") return;
        var transaction = db.transaction(["people"],"readonly");
        var store = transaction.objectStore("people");
        var request = store.get(Number(key));
        request.onsuccess = function(e) {
            var result = e.target.result;
            if(result) {
                var s = "<h2>Forma broj: "+key+"</h2><p>";
                for(var j=0; j<result.length; j++) {
                        s+= (result[j].pitanje + ': ' + result[j].odgovor  + "<br/>");
                }
                document.querySelector("#status").innerHTML = s;
            }
            else {
                document.getElementById('element').style.visibility='visible'
            }
        }
    }
    // get allPeople
    
    
    
    // Funkcija za pravljenje Elemenata
    var i = 0;
    var a = 1;
    function mojaFunkcija() {
        var type1 = document.getElementById('type1').value;
        var type2 = document.getElementById('type2').value;
        var question = document.getElementById('question').value;
        var counter = 'Element';
        counter+= a;
        var prviElement=document.createElement('span');
        prviElement.textContent= counter + ':' +'  ';
        document.body.appendChild(prviElement);
        var pitanje= document.createElement('span');
        var unos='unos';
        unos += i;
        pitanje.id=unos;
        pitanje.textContent=question + ' ';
        document.body.appendChild(pitanje);
        var tip1 = document.createElement("input");
        var element='element';
        element += i;
        tip1.id=element;
        if (type1=='textbox') {
            tip1.type=type2
        } else {
            tip1.type=type1
        }
        document.body.appendChild(tip1);
        var linija1= document.createElement("br");
        document.body.appendChild(linija1);
        var linija2= document.createElement("br");
        document.body.appendChild(linija2);
        i++;
        a++;
    }
    
    function AddTextBox(elm) {
        var v = elm.value;
        var iCounter = elm.id.replace('type1', '');
    
        if (v == 'radio') {
            var textbox = document.createElement('input');
            textbox.type = 'text';
            textbox.id = 'txtSecond' + iCounter;
            elm.parentNode.insertBefore(textbox, elm.nextSibling);
        } else {
            //Ovaj kod ce da izbrise Textbox u slucaju da se izabere druga opcija.
            var rmv = document.getElementById('txtSecond' + iCounter);
            if (rmv != undefined) {
                rmv.remove();
            }
        }
    }
    
    function modify_value()
    {
        var hidden_field = document.getElementById('test3');
        hidden_field.value = 'testvalue';
    
    }
    /* Dropdown Button */
    .dropbtn {
        background-color: #A9A9A9;
        color: white;
        padding: 10px;
        font-size: 16px;
        text-align:center;
        border: none;
        cursor: pointer;
    }
    /* Dropdown button on hover & focus */
    .dropbtn:hover, .dropbtn:focus {
        background-color: #696969;}
    input{
        text-align:left;
    }
    
    .hide {
        visibility: hidden
    }
    
    .h1 {
        font-size: 4em;
    }
    
    .h2 {
        font-size: 2.5em;
    }
    
    /* Dropdown Button */
    .dropbtn2 {
        background-color: #A9A9A9;
        color: white;
        padding: 10px;
        font-size: 16px;
        text-align:center;
        border: none;
        cursor: pointer;
        margin-left:580px;
    }
    /* Dropdown button on hover & focus */
    .dropbtn2:hover, .dropbtn2:focus {
        background-color: #696969;}
    input{
        text-align:left;
    }
    
    .kocka {
        max-width: 291px;
        max-height: 94px;
        background-color: #91ceff;
        border: 20px;
        border-style:solid;
        border-color:#91ceff;
    
    }
    
    .kocka2 {
        max-width: 291px;
        max-height: 94px;
        background-color: #91ceff;
        border: 20px;
        border-style:solid;
        border-color:#91ceff;
    
    }
    /* Dropdown button on hover & focus */
    .kocka2:hover, .kocka2:focus {
        background-color: #696969;}
    input{
        text-align:left;
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <link rel="stylesheet" type="text/css" href="pepo.css">
        <meta charset="UTF-8">
        <script src="basa.js"></script>
        <title>OnlineForms</title>
        </head>
    <body>
        <!--Ovaj kod je za Main Page-->
        <Main class="kocka">
            <div align="left">
                <input type="button" class="dropbtn" value="Administration" onClick="window.location.reload();return false;"/>
                &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
                <button class="dropbtn" id="getAllButton">Forms</button>
            </div>
            <br>
            <div align="left">
                <input type="number" id="key" class="dropbtn"  Placeholder="Type the key value"/>
                <button class="dropbtn" id="getButton"> Search </button> 
                <br>
                <br>
            </div>
        </Main>
        <!--Ovaj kod je za Elemente-->
        <div id="element" class="hide">
                <h1>Element <input type="text" class="dropbtn" id="question" value="" Placeholder="Type your question here"/>
                <select title="ddmenu" class="dropbtn" id="type1">
                    <option selected disabled hidden value="Please select">Please select</option>
                    <option value="textbox">textbox</option>
                    <option value="checkbox">checkbox</option>
                    <option value="radio">radio button</option>
                    </select>
                <select title="ddmenu" class="dropbtn" id="type2">
                    <option selected disabled hidden value="Please select">Please select</option>
                    <option value="none">none</option>
                    <option value="mandatory">mandatory</option>
                    <option value="number">numeric</option>
                </select>
                </h1>
            <input type="button"  id="adddugme" class="dropbtn2" value="Add"  onclick="mojaFunkcija()"/>
            <br>
            <br>
            <button id="addButton" class='dropbtn'>Save</button>
            </div>
        <br>
        <br>
    <div id="status"></div>
        <br>
    <div id="status2"></div>
    </body>
    </html>

0 个答案:

没有答案