简体   繁体   English

JavaScript函数似乎不能一致地工作

[英]JavaScript function doesn't seem to work consistently

I'm trying to create a dynamic menu that only displays certain questions if the previous question has been answered. 我正在尝试创建一个动态菜单,只有在前一个问题得到解答后才会显示某些问题。 I've got it to work for one of the questions, but the next one throws the error Uncaught TypeError: Cannot read property 'length' of undefined , even though the code is virtually identical. 我已经让它为其中一个问题工作,但下一个抛出错误Uncaught TypeError: Cannot read property 'length' of undefined ,即使代码实际上是相同的。

Here's the JavaScript function I'm calling: 这是我正在调用的JavaScript函数:

// displays div when input is not empty
function showOnInput(div, input) {
    let x = document.getElementById(`${input}`);
    let y = document.getElementById(`${div}`);
    if (x.value.length > 0) {
        y.style.display = 'block';
    } else {
        y.style.display = 'none';
    }
}

and here's the HTML code it's being called in: 这里是调用它的HTML代码:

<div id="customizeChar" class="text">
    <form>
    <div id="name">
        <label>What is your character's name?</label>
        <br>
        <input v-model="fname" id="fname" placeholder="First Name" onkeyup="showOnInput('age', 'fname')">
        <input v-model="lname" id="lname" placeholder="Last Name">
    </div>
    <br>
    <div id="age" style="display: none">
        <label>How old is {{fname}} {{lname}}?</label>
        <br>
        <input v-model="age" id="age" placeholder="Age" onkeyup="showOnInput('sex', 'age')">
        <label style="font-size: 80%">years old</label>
    </div>
    <br><br>
    <div id="sex" style="display: none">
        <label>Is {{fname}} a male or female?</label>
        <br>
        <select v-model="sex">
            <option>Male</option>
            <option>Female</option>
        </select>
    </div>
    </form>
</div>

The age block is hidden until an input is given in the name block, as expected, but inputting an answer in the age block then throws the Uncaught TypeError error. 隐藏age块,直到在name块中给出输入,如预期的那样,但是在age块中输入答案然后抛出Uncaught TypeError错误。

I was able to get this to do what I wanted using Vue's v-if directive. 我能够使用Vue的v-if指令让它做我想做的事。 Here's the final HTML: 这是最终的HTML:

<div id="customizeChar" class="text">
    <form>
    <div id="name">
        <label>What is your character's name?</label>
        <br>
        <input v-model="fname" id="fname" placeholder="First Name">
        <input v-model="lname" id="lname" placeholder="Last Name">
    </div>
    <br>
    <div id="age" v-if="fname != ''">
        <label>How old is {{fname}} {{lname}}?</label>
        <br>
        <input v-model="age" id="age" placeholder="Age">
        <label style="font-size: 80%">years old</label>
    </div>
    <br><br>
    <div id="sex" v-if="age != ''">
        <label>Is {{fname}} a male or female?</label>
        <br>
        <select v-model="sex">
            <option>Male</option>
            <option>Female</option>
        </select>
    </div>
    </form>
</div>

Let me know if there are improvements I can make. 如果我能做出改进,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM