javascript - 为什么我的
问题描述
我希望只有在填写完字段后才会出现“查看您的输入”按钮。但努力使按钮出现。请指教。
function notify() {
alert("Welcome to my form");
var name = prompt("Enter your name");
var city = prompt("Which city you are living in");
var mobile = prompt("Enter your phone number");
}
function nextStep() {
var a = document.getElementById("showUp");
if (a.style.display === "none") {
a.style.display = "block";
document.getElementById("name").innerHTML = "Your name is " + name;
document.getElementById("city").innerHTML = "Your city is " + city;
document.getElementById("mobile").innerHTML = "Your phone number is " + mobile;
} else {
x.style.display = "none";
}
}
<p>Click the button to display an alert box.</p>
<button onclick="notify()">Try it</button>
<div id="showUp" style="display: none;">
<button onclick="nextStep()">See your input</button>
</div>
<p id="name"></p>
<p id="city"></p>
<p id="mobile"></p>
解决方案
没有执行该功能nextStep()
。
您需要nextStep()
在提示后调用该函数。
您已将功能绑定到单击按钮。但是因为用户看不到按钮,所以它不会运行。
一个函数只有在定义后才调用它来执行。
function myFunc() {
// do thing
}
myFunc(); // This executes the function above.
function notify() {
alert("Welcome to my form");
var name = prompt("Enter your name");
var city = prompt("Which city you are living in");
var mobile = prompt("Enter your phone number");
nextStep(); // Add
}
或者,您可以将showUp()
显示按钮的函数中的逻辑移到notify()
函数中。
var name;
var city;
var mobile;
function notify() {
alert("Welcome to my form");
name = prompt("Enter your name");
city = prompt("Which city you are living in");
mobile = prompt("Enter your phone number");
showButton();
}
function showButton() {
var a = document.getElementById("showUp");
if (a.style.display === "none") {
a.style.display = "block";
} else {
a.style.display = "none";
}
}
function nextStep() {
document.getElementById("name").innerHTML = "Your name is " + name;
document.getElementById("city").innerHTML = "Your city is " + city;
document.getElementById("mobile").innerHTML = "Your phone number is " + mobile;
}
推荐阅读
- google-apps-script - 在 Google 表格中查询和转置数据
- vue.js - v-if="this.canupdate == true" 不工作
- python - 如何显示计数和速率之间的相关性?
- r - 按组可视化混合模型中随机效应的方差
- mysql - 发生异常。_TypeError(类型'列表
' 不是类型 'Map 的子类型 ') - amazon-web-services - 使用非默认密钥通过 SSH 连接到 AWS Lightsail 时出现问题
- angular - 将 eventListener 添加到 Angular 中的多个项目
- python - 如何获取在 cv2.warp 方法中“从无到有”扭曲的像素的蒙版?
- css - 显示 SVG 图标的 Angular 可重用组件的宽度/高度未在任何地方设置并位于图标正下方,stackblitz 内部
- laravel - Laravel 雄辩的独特选择与调用关系的可能性?