Javascript函数立即停止

时间:2018-05-30 15:58:01

标签: javascript html function dom

我有一个功能,它启用按钮2(id keuze1)和按钮3(id keuze 3)如果我按下按钮1(id = naam)。

function init() {
  let startKnop = document.getElementsByTagName("button")[0].addEventListener("click", startClicked, false);
}

function startClicked(event) {
  let knop2 = document.getElementById("keuze1");
  knop2.removeAttribute("disabled");
  let knop3 = document.getElementById("keuze2");
  knop3.removeAttribute("disabled");
  toonVraag();
}

function toonVraag(event) {
  let i = vraagTeller.value;
  let vraag = document.getElementById('vraag');
  vraag.innerHTML = "Hello World";
}
<label for="naam">Code: </label><input type="text" id="naam" />
</div>
<button disabled>Start</button>
<div>
  <p id="vraag">Een vraag</p>
  <button id="keuze1" disabled>Keuze1</button>
  <button id="keuze2" disabled>Keuze2</button>
</div>

因此它成功运行函数startClicked(),但立即停止。 如果我添加代码event.preventDefault(),则不会更改我的元素(id = vraag)中的文本。

1 个答案:

答案 0 :(得分:1)

似乎工作正常,如this JSFiddle

中所示

我做了一些清理工作:

  • 从第一个按钮
  • 中删除了disabled属性
  • 从第2行删除了</div>
  • 添加了对init()功能
  • 的调用

因此:

&#13;
&#13;
function init() {

    let startKnop = document.getElementsByTagName("button")[0].addEventListener("click", startClicked, false);

}

init();

function startClicked(event) {
    let knop2 = document.getElementById("keuze1");
    knop2.removeAttribute("disabled");

    let knop3 = document.getElementById("keuze2");
    knop3.removeAttribute("disabled");

    toonVraag();
}

function toonVraag(event) {

    let vraag = document.getElementById('vraag');
    vraag.innerHTML = "Hello World";

}
&#13;
<label for="naam">Code: </label><input type="text" id="naam" />
<button>Start</button>
<div>
    <p id="vraag">Een vraag</p>
    <button id="keuze1" disabled>Keuze1</button>
    <button id="keuze2" disabled>Keuze2</button>
</div>
&#13;
&#13;
&#13;