程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Javascript表单验证返回false,但表单仍然提交

发布于2021-06-16 05:59     阅读(1188)     评论(0)     点赞(29)     收藏(3)


我正在尝试使用 javascript 验证表单。该函数被正确调用并显示警报,但在我单击警报后仍提交表单。该函数从不返回 false。我之前看过这个问题,但我还没有看到有效的答案。我正在使用一些 JQuery 移动设备,这可能是问题所在。我试图更改按钮输入类型=按钮,但这甚至不会提交表单。

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 绑定事件

$(function () {
        $('#form').bind('submit', function (e) {
            var isValid = true;
            if (!isValid) {
                e.preventDefault();
            }
        });
    });



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.qianduanheidong.com/blog/article/129651/c2c4028b8b335295e6c0/

来源:前端黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

29 0
收藏该文
已收藏

评论内容:(最多支持255个字符)