我有一个带有输入框和按钮的网页。如果用户输入了一些值(12345),则对象需要显示在下面或代替该表单(输入框和按钮)。
我正在处理一个值检查,我的整个代码如下所示:
static long dateTimeDifference(Temporal d1, Temporal d2, ChronoUnit unit){
return unit.between(d1, d2);
}
目前,此代码在新窗口中显示对象(仅页面上的对象)。
这也是offtopic但是如果你可以提供帮助,如果按下enter来激活功能<form name="form" action="" method="post">
<input type="text" name="subject" id="subject" placeholder="UNESI KOD">
</form>
<button onclick="proveriKljuc()" style="margin-top:20px">Potvrdi!</button>
<script>
function proveriKljuc() {
if (document.getElementById("subject").value == 12345) {
document.write(
"<center><object id='object1' data='http://termodom.rs/wp-content/uploads/2016/12/akcija1-1.jpg'></object></center>"
);
}
}
</script>
,我该如何处理呢?
答案 0 :(得分:0)
请勿使用document.write
。如果要替换当前表单:
<div id="wrapper">
<form name="form" action="" method="post">
<input type="text" name="subject" id="subject" placeholder="UNESI KOD">
</form>
</div>
<button onclick="proveriKljuc()" style="margin-top:20px">Potvrdi!</button>
<script>
function proveriKljuc()
{
if(document.getElementById("subject").value == 12345)
{
document.getElementById("wrapper").innerHTML = "<center><object id='object1' data='http://termodom.rs/wp-content/uploads/2016/12/akcija1-1.jpg'></object></center>";
}
}
</script>
如果您希望对象显示在表单下方:
<div id="wrapper">
<form name="form" action="" method="post">
<input type="text" name="subject" id="subject" placeholder="UNESI KOD">
</form>
</div>
<button onclick="proveriKljuc()" style="margin-top:20px">Potvrdi!</button>
<script>
function proveriKljuc()
{
if(document.getElementById("subject").value == 12345)
{
document.getElementById("wrapper").innerHTML += "<center><object id='object1' data='http://termodom.rs/wp-content/uploads/2016/12/akcija1-1.jpg'></object></center>";
}
}
</script>
答案 1 :(得分:0)
页面加载后永远不能使用document.write。它会擦除页面。而是有div或span并填充它的innerHTML或者使用document.createElement创建的div的appendChild。返回false onsubmit以停止提交或按下按钮type="button"
- 或者显示隐藏的div How to show hidden div after hitting submit button in form?
我无法展示一个有效的例子,因为stacksnippets不支持提交
window.onload = function() {
document.getElementById("form").onsubmit = function() {
if (this.elements["subject"].value == 12345) {
document.getElementById("objContainer").innerHTML = "<center><object id='object1' data='http://termodom.rs/wp-content/uploads/2016/12/akcija1-1.jpg'></object></center>";
}
return false; // ignore submit
}
}
<form id="form" action="" method="post">
<input type="text" name="subject" id="subject" placeholder="UNESI KOD">
<button type="submit" style="margin-top:20px">Potvrdi!</button>
</form>
<div id="objContainer"></div>