gpt4 book ai didi

javascript - 如何在 Vanilla JavaScript 中单击按钮时获取表单输入值?

转载 作者:行者123 更新时间:2023-11-30 11:27:09 25 4
gpt4 key购买 nike

我试图摆脱 jQuery,但陷入了一些看似简单的事情:我只想在单击按钮时使用输入值更新段落文本。我没有收到控制台错误,但不确定我在哪里搞砸了,因为输入值没有通过。

编辑:我的问题是我希望在单击按钮时将名称和作业输入值附加到

内容,目前这种情况没有发生,但我也没有收到控制台错误。

fiddle :https://jsfiddle.net/cdcampbell26/3z79ewhm/

var nameValue = document.querySelector( ".input-name" ).value;
var jobValue = document.querySelector( ".input-job" ).value;
var resultsBlock = document.querySelector( ".results" );
var resultName = document.querySelector( ".name-result" );
var resultJob = document.querySelector( ".job-result" );


function showSignature() {
resultName.innerHTML += nameValue;

resultJob.innerHTML += jobValue;

resultsBlock.classList.remove( "hidden" );
}

document.querySelector( ".show-signature" ).addEventListener( 'click', function(){
showSignature();
});
/* Main Styles for Email Signature Generator */
html, body {
height: 100%;
width: 100%;
min-height: 100%;
}

/* Hide the Signature results initially */
.hidden {
opacity: 0;
visibility: hidden;
}

.visible {
opacity: 1;
visibility: visible;
}

.main-ui {
background: #fefefe;
height: 100vh;
width: 100vw;
}

.container {
align-items: center;
display: flex;
flex-direction: column;
justify-content: center;
}

.text-input {
-webkit-appearance: none;
backface-visibility: hidden;
border: 1px solid #ccc;
border-radius: 4px;
box-shadow: none;
clear: both;
float: left;
margin-bottom: 40px;
padding: 10px;
transition: border 0.3s, transform 0.3s;
height: 30px;
width: 400px;
}

.text-input:focus,
.text-input:hover {
border: 1px solid purple;
outline: none;
transform: scale(1.1);
}

.show-signature {
background: purple;
border: none;
border-radius: 50px;
box-shadow: none;
color: #fff;
clear: both;
float: left;
padding: 20px;
transition: background 0.3s, transform 0.3s;
-webkit-appearance: none;
}

.show-signature:hover,
.show-signature:focus {
background: #222;
transform: scale(1.1);
}
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>WebDevStudios Email Signature Generator</title>
<link rel='stylesheet' href='css/main.css'>

</head>
<body id="signature-generator">

<main class="main-ui container">

<section class="input-container">
<input type="text" placeholder="What's your name?" value="" id="name" class="text-input input-name">
<input type="text" placeholder="What's your job title?" value="" id="job" class="text-input input-job">
<button class="show-signature">Gimme My Signature</button>
</section>

<section class="results hidden">
<!-- TABLE RESULTS BASED ON INPUTS -->

<p class="name-result">Your name is </p>
<p class="job-result">Your job is </p>

</section>

</main>

<script src="js/main.js"></script>
</body>
</html>

最佳答案

代码的问题在于您在第 1 行和第 2 行初始化时获取值并将它们分配给这些变量。

当你执行input.value时,值就是最终值,如果你改变浏览器上的输入内容,之前分配的值不会改变。

如果您想要一些动态值,您必须将其查询推迟到您需要的时刻。

因此,根据您的代码,解决方案将如下所示:

var nameInput = document.querySelector( ".input-name" );
var jobInput = document.querySelector( ".input-job" );
var resultsBlock = document.querySelector( ".results" );
var resultName = document.querySelector( ".name-result" );
var resultJob = document.querySelector( ".job-result" );


function showSignature() {
resultName.innerHTML += nameInput.value;

resultJob.innerHTML += jobInput.value;

resultsBlock.classList.remove( "hidden" );
}

这是 jsfiddle 的链接,显示它正在运行 https://jsfiddle.net/cdcampbell26/3z79ewhm/

关于javascript - 如何在 Vanilla JavaScript 中单击按钮时获取表单输入值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47579182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com