我正在尝试使用 javascript 验证表单。该函数被正确调用并显示警报,但在我单击警报上的确定后仍然提交了表单。该函数从不返回 false。我以前见过这个问题,但我没有看到有效的答案。我在这里使用了一些 JQuery Mobile,这可能是问题所在。我试图更改按钮 input type=button,但那甚至不会提交表单。
javascript函数是:
<script>
function validateInventoryform()
{
var sku=document.getElementById('sku_checkbox');
var entire=document.getElementById('entire_checkbox');
var inv=document.getElementById('inv');
if ((sku.checked==true && entire.checked==true))
{
alert("You may only choose one option to check customer inventory");
return false;
}
}
</script>
形式是:
<div data-role="popup" id="popupQuery_inventory" data-theme='a' >
<div style="padding:10px 20px;">
<form action="inventory_inquiry.php" method="get" id="inv" onsubmit="return validateInventoryform()" >
<h3>Enter SKU</h3>
<fieldset data-role="controlgroup" data-type="horizontal" data-mini="true">
<legend>Choose Input Type:</legend>
<input type="checkbox" data-theme="a" name="sku_checkbox" id="sku_checkbox" value="off" checked="checked">
<label for="sku_checkbox">SKU</label>
<input type="checkbox" data-theme="a" name="entire_checkbox" id="entire_checkbox" value="off">
<label for="entire_checkbox">Entire Inventory</label>
</fieldset>
<label for="sku" class="ui-hidden-accessible">SKU:</label>
<input type="text" name="sku" id="sku" value="" placeholder="Item SKU" data-theme="a">
<input name="customer_id" type="hidden" value='<?php echo $customer_id; ?>'/>
<button type="submit" data-theme="b"/>Submit</button>
</form>
</div>
</div>
我正在添加调用表单弹出窗口的 href。我不知道这是否与这个问题有关。我一直听说我的代码正在被复制并且工作正常。我无法让它发挥作用,我想完整地展示这个问题。
<ul data-role="listview" data-inset="true" data-theme="b">
<li><a href="#popupCustomer_alert" data-rel="popup" data-position-to="origin" data-inline="true" data-transition="pop" >Add Customer Alert</a></li>
<li><a href="#popupQuery_inventory" data-rel="popup" data-position-to="origin" data-inline="true" data-transition="pop" >Query Inventory</a></li>
<li><a href="#popupEdit_customer" data-rel="popup" data-position-to="origin" data-inline="true" data-transition="pop" >Edit Customer</a></li>
<li><a href="return_qr_code.php" >Get QR Code</a></li>
<li><a href="#popupDownload_inventory" data-rel="popup" data-position-to="origin" data-inline="true" data-transition="pop" >Download Inventory</a></li>
</ul>
最佳答案
尝试在 validateInventoryform() 的最后一行添加 return true 并删除多余的“/”
<button type="submit" data-theme="b"/>Submit</button>
尝试Jquery绑定(bind)事件
$(function () {
$('#form').bind('submit', function (e) {
var isValid = true;
if (!isValid) {
e.preventDefault();
}
});
});
关于Javascript 表单验证返回 false,但表单仍然提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17205658/