javascript - 单击提交按钮后如何根据文本值调整文本框的大小?
问题描述
在文本框中输入值并按下提交按钮后,如何根据里面的值调整文本框的大小。所以文本中心对齐显示正确(使用纯 javascript 或 css)。
<!DOCTYPE html>
<body>
<div class="block">
☀
<input type="text" id="inputValue" placeholder="Change City"></input>
<button type="submit" id="submit">Submit</button>
</div>
<style>
.block{
border: 2px solid;
width: 300px;
height: 50px;
display: flex;
justify-content: center;
align-items: center;
}
#inputValue{
border: none;
}
#submit{
display: none;
}
</style>
<script>
var inputValue = document.querySelector("#inputValue");
var submit = document.querySelector("#submit");
inputValue.addEventListener("click",function(){
submit.style.display = "block";
});
submit.addEventListener("click",function(){
submit.style.display = "none";
});
</script>
</body>
</html>
解决方案
我给你举个例子。从这里开始的想法
<input class="txt" id="inputBox" type="text"><br>
<button onClick=test()>test</button>
function test(){
var getClass = document.querySelector( ".txt" );
var getValue = document.getElementById("inputBox").value.length
getClass.style.width = ((getValue + 1) * 8) + 'px';
}
除了这是另一种方法之外,这对您来说会更好。 点击这里
function test(){
var getClass = document.querySelector( ".txt" );
var getValue = document.getElementById("inputBox").value.length
getClass.style.width = ((getValue + 1) * 8) + 'px';
}
<input class="txt" id="inputBox" type="text"><br>
<button onClick=test()>
test
</button>
推荐阅读
- python - “subprocess.call”不起作用。错误消息:“[WinError 193] %1 不是有效的 Win32 应用程序”
- python - 如何让图像显示在我的 django 应用程序上?
- python - 为什么我的程序不读取文件的下一行?
- node.js - 如何使用 node-rsa 后端加密/解密 Json 数组:NodeJS 和前端:Angular
- python - 计算数组之间的距离
- java - Groovy 的 Getter 和 Setter 快捷方式符号错误地覆盖了函数调用
- undefined - 在 Isabelle/HOL 中公理化“未定义”的原因是什么?
- swift - 如何在 Swift 中隐藏光标?
- r - 如何在传单中自定义缩放级别
- javascript - 遍历数组并执行函数