Javascript Don';在确认窗口中键入“否”时不发送表单数据
在名称为Javascript Don';在确认窗口中键入“否”时不发送表单数据,javascript,confirm,Javascript,Confirm,在名称为goods的输入中键入apple,在名称为price的输入中键入9,然后单击submit,现在确认窗口弹出,无论您单击yes或no,数据都将发送到price.php 我的期望: 单击yes,数据将发送到price.php, 当你点击no,数据将不会发送到price.php,我的js怎么了 ob=document.getElementById(“提交”); 函数检查(){ if(document.getElementById(“price”).value
goods
的输入中键入apple
,在名称为price
的输入中键入9
,然后单击submit
,现在确认窗口弹出,无论您单击yes
或no
,数据都将发送到price.php
我的期望:
单击
yes
,数据将发送到price.php
,
当你点击no
,数据将不会发送到price.php
,我的js怎么了
ob=document.getElementById(“提交”);
函数检查(){
if(document.getElementById(“price”).value<10){
var flag=window.confirm(“您确定价格低于10吗?”);
国际单项体育联合会(旗){
返回true;
}否则{
出口
}
}
}
ob.addEventListener(“单击”,检查,错误);
商品
价格
这是我的解决方案:
<html>
<body>
<form action="price.php" method="post" onsubmit="return check()">
<table>
<tr>
<td>goods</td>
<td><input type="text" name="goods"></td>
</tr>
<tr>
<td>price</td>
<td><input type="text" id="price" name="price"></td>
</tr>
<tr><td colspan=2><input type="submit" id="submit" value="submit"></td></tr>
</table>
</form>
<script>
function check()
{
if(document.getElementById("price").value < 10){
var flag = window.confirm(" are your sure the price is less than 10 ?");
if(flag){
return true;
}else{
return false;
}
}
}
</script>
</body>
</html>
<html>
<body>
<form action="price.php" method="post" id="form">
<table>
<tr>
<td>goods</td>
<td><input type="text" name="goods"></td>
</tr>
<tr>
<td>price</td>
<td><input type="text" id="price" name="price"></td>
</tr>
<tr><td colspan=2><input type="submit" id="submit" value="submit"></td></tr>
</table>
</form>
<script>
var ob = document.getElementById('form');
function check(event){
if(document.getElementById("price").value < 10){
var flag = window.confirm(" are your sure the price is less than 10 ?");
if(flag){
ob.submit();
return true;
}else{
event.preventDefault();
return false;
}
}
}
ob.addEventListener("submit",check);
</script>
</body>
</html>
商品
价格
函数检查()
{
if(document.getElementById(“price”).value<10){
var flag=window.confirm(“您确定价格低于10吗?”);
国际单项体育联合会(旗){
返回true;
}否则{
返回false;
}
}
}
我在Edge、IE11、Firefox和chrome浏览器上测试了它,它可以正常工作
我找到了另一个解决方案:
<html>
<body>
<form action="price.php" method="post" onsubmit="return check()">
<table>
<tr>
<td>goods</td>
<td><input type="text" name="goods"></td>
</tr>
<tr>
<td>price</td>
<td><input type="text" id="price" name="price"></td>
</tr>
<tr><td colspan=2><input type="submit" id="submit" value="submit"></td></tr>
</table>
</form>
<script>
function check()
{
if(document.getElementById("price").value < 10){
var flag = window.confirm(" are your sure the price is less than 10 ?");
if(flag){
return true;
}else{
return false;
}
}
}
</script>
</body>
</html>
<html>
<body>
<form action="price.php" method="post" id="form">
<table>
<tr>
<td>goods</td>
<td><input type="text" name="goods"></td>
</tr>
<tr>
<td>price</td>
<td><input type="text" id="price" name="price"></td>
</tr>
<tr><td colspan=2><input type="submit" id="submit" value="submit"></td></tr>
</table>
</form>
<script>
var ob = document.getElementById('form');
function check(event){
if(document.getElementById("price").value < 10){
var flag = window.confirm(" are your sure the price is less than 10 ?");
if(flag){
ob.submit();
return true;
}else{
event.preventDefault();
return false;
}
}
}
ob.addEventListener("submit",check);
</script>
</body>
</html>
商品
价格
var ob=document.getElementById('form');
功能检查(事件){
if(document.getElementById(“price”).value<10){
var flag=window.confirm(“您确定价格低于10吗?”);
国际单项体育联合会(旗){
ob.submit();
返回true;
}否则{
event.preventDefault();
返回false;
}
}
}
ob.addEventListener(“提交”,检查);
传统的方式与KNVB相同,关键是
,将表单的属性绑定到submit
上,使用函数check
DOM0级事件方式,与传统方式几乎相同
<html>
<body>
<form action="price.php" method="post" id="form">
<table>
<tr>
<td>goods</td>
<td><input type="text" name="goods"></td>
</tr>
<tr>
<td>price</td>
<td><input type="text" id="price" name="price"></td>
</tr>
<tr><td colspan=2><input type="submit" id="submit" value="submit"></td></tr>
</table>
</form>
<script>
var ob = document.getElementById('submit');
ob.onclick =function(){
if(document.getElementById("price").value < 10){
var flag = window.confirm(" are your sure the price is less than 10 ?");
if(flag){
return true;
}else{
return false;
}
}
}
</script>
</body>
</html>
2.当标志为false时
else{
event.preventDefault();
return false;
}
关于代码,有两点:
退出
-我以前从未见过-是javascript吗document.getElementById('price')。value
-返回一个字符串-您应该在比较之前将其解析为一个数字onsubmit=“
属性-返回true
允许表单提交,使用false
阻止提交window.confirm
已返回布尔值,只需返回该值(而不是if
语句)函数验证(){
const price=parseFloat(document.getElementById('price').value)
如果(价格<10){
返回窗口。确认(“您确定价格低于10吗?”)
}
return true//您的js代码工作正常,为什么我的js代码不能按预期工作?他的代码工作正常,因为表单标记具有onsubmit=“return check()”属性。如果返回false,则会阻止提交。您不会将“onsubmit”事件与验证函数关联。ob.addEventListener(“onsubmit”,check,false);
无法在我的js代码中工作。变量ob是submit button对象,您应该将submit事件侦听器添加到表单对象,而不是submit button对象,但是,我以前没有尝试过,我不能保证它能按您的要求工作。