当我启动该功能时,不能将值插入输入或使值保持不变

时间:2016-09-09 21:24:52

标签: javascript

我正在处理一个小小的数学任务,我可以在这里对一个数字进行推理,这里是24.但是这行input2.value = intNumber2;无效。所以我写的数字消失了,我不能使用javascript来插入input2.value = intNumber2;的值。 所以我希望我写的值保留在输入中,或者我想在之后插入。

http://codepen.io/anon/pen/jrbyBX

    <div id="showmessage" class="showmessage"></div>
<div id="form">

<p>
<input type="number" name="input01" id="input01" value="24" disabled>
</p>
<p>
<input type="number" name="input11" id="input11"> <input type="number" name="input12" id="input12">
</p>
</div>  

<p>
<input type="submit" onclick="checknumber();" value="neste">
</p>


</body>
<script language="javascript">
var intLinenr = 1;
var intNumber1 = 11;
var intNumber2 = 12;
var intNumberAns = 01;


function checknumber() {
var boovirker = 1;
var strmessage = "";
var div = document.getElementById("form");
var input1 = document.getElementById("input" + intLinenr + "1");
var input2 = document.getElementById("input" + intLinenr + "2");
if (intLinenr === undefined){intLinenr = 1;}//Sjekker om variabelen er definert
console.log("intLinenr = " + intLinenr)
intNumber1 = input1.value;
intNumber2 = input2.value;
intNumberAns = document.getElementById("input" + (intLinenr - 1) + "1").value;





//-----------------------------------------Sjekker om noen av tallene er primtall---------------------------------------
if (isPrime(intNumber1) == false && isPrime(intNumber2) == false){strmessage = "Ingen av tallene er primtall";boovirker = 0;}
//-----------------------------------------Sjekker om det blir riktig ganging-------------------------------------------
if (intNumber1*intNumber2 != intNumberAns){boovirker = 0; strmessage += "<br>" + intNumber1 + " multiplisert med " + intNumber2 + " blir ikke " + intNumberAns}

//---------------------------------------ALT ER RIKTIG------------------------------------------------------------------
if (boovirker == 1){
    console.log("document.getElementById('input" + intLinenr + "1').value = " +intNumber1);

    console.log("Input01 = " + input1.value);
    document.getElementById("input01").value = "Ja";
    input1.value = '1';
    input2.value = intNumber2;
    input1.disabled = "true";
    input2.disabled = "true";
    document.getElementById("input" + intLinenr + "2").style.disabled = "true";

intLinenr = intLinenr + 1;
form.innerHTML = form.innerHTML + "<p><input type=\"number\" class=\"\" name=\"\" id=\"input"+ intLinenr +"1\" value=\"\"> <input type=\"number\" name=\"\" id=\"input"+ intLinenr +"2\" value=\"\"></p>"
    //return false;
    }




document.getElementById("showmessage").innerHTML = strmessage;//Gir beskjed hvis feil
strmessage = "";//Nuller ut beskjeden
//return false;
}
function isPrime(value) {
    for(var i = 2; i < value; i++) {
        if(value % i === 0) {
            return false;
        }
    }
    return value > 1;
}
</script>

编辑:如果你不知道什么是primefactorization,你可以尝试在两个框中插入3和8。

编辑2: 问题在于这一行:form.innerHTML = form.innerHTML + "<p><input type=\"number\" class=\"\" name=\"\" id=\"input"+ intLinenr +"1\" value=\"\"> <input type=\"number\" name=\"\" id=\"input"+ intLinenr +"2\" value=\"\"></p>"。当我删除这条线3和8没有消失。 但是我该怎么做才能解决这个问题呢?

1 个答案:

答案 0 :(得分:0)

在这里找到答案:HTML Upload Form Resetting After innerHTML change

并使用类似的东西

var fileInput = document.getElementById('fileInput'),
p = document.createElement('P');
p.innerHTML = 'Something';
fileInput.parentNode.insertBefore(p, fileInput.nextSibling);