简体   繁体   English

禁用提交按钮

[英]Disabling the Submit button

I have a form which contains several elements and one of them being the select value element.However what i have done is that i have attached the quantity that has to be shown on the select menu, whose values comes from the database. 我有一个包含几个元素的表单,其中一个是选择值元素。但是我所做的是我已经附加了必须在选择菜单上显示的数量,其数量来自数据库。

Example: 例:

Suppose i have set 10 in my database for quantity then , the select element will show me options from 1-10. 假设我在数据库中设置了10个数量,那么select元素将显示1-10个选项。

Code: 码:

               <?php
                    if(@$dbqty>=10)
                    {
                        $selectbox='<p> Quantity:  <select name="product_qty">';
                        for($i=1;$i<=10;$i++)
                        {
                            $selectbox.='<option value="'.$i.'">'.$i.'</option>';
                        }
                        $selectbox.='</select></p>';
                        echo $selectbox;
                    }
                    else if(@$dbqty<10 && @$dbqty>0)
                    {
                        $selectbox='<p> Quantity:  <select name="product_qty">';
                        for($i=1;$i<=@$dbqty;$i++)
                        {
                            $selectbox.='<option value="'.$i.'">'.$i.'</option>';
                        }
                        $selectbox.='</select></p>';
                        echo $selectbox;

                    }
                    if(@$dbqty==null || @$dbqty==0)
                    {
                        echo '<input type="button" name="product_qty" value="Sold Out" disabled="disabled"/>';
                    }
                    ?>

In the javascript part i have set a function which submit the form to a php file and loads its response text. 在javascript部分中,我设置了一个函数,该函数将表单提交到php文件并加载其响应文本。

Code: 码:

$(document).ready(function(){
$(document).on('submit','#submitform',function(event){
    event.preventDefault();
    var button_content = $(this).find('button[type=submit]');
    button_content.html('Adding...');
    var data=$(this).serialize();
    $.ajax({
        type:'POST',
        url:'../cart/index.php',
        data:data,
        success : function(content)
        {
            if ($('#ajaxview').find('#popupcart')) {
                $('#popupcart').hide();

                $('#ajaxview').append(content);
                button_content.html('Add');
            }
            else
            {
                $('#ajaxview').append(content);
                button_content.html('Add');
            }

        }
    })
})
})    

What i was trying to do is that when the quantity for the item comes out to be sold out the submit button gets disabled.Is it possible? 我试图做的是,当该项目的数量被卖完时 ,提交按钮被禁用。是否可以? If yes,How to do this one then? 如果是,该怎么办?

Thanks! 谢谢!

我认为,更好的方法是使用javascript从数据库中获取数据,然后根据检索到的值,使用jQuery启用/禁用按钮。

use this jquery to remove the submit events on the buttons which has value sold out 使用此jQuery删除按钮上已售罄的提交事件

 $('input[value="Sold Out"]').on('click',function(e){
   e.preventDefault(); //stop the event
});

Or if the elements are appended dynamically, then you got to use delegated event handlers to attach the event. 或者,如果元素是动态附加的,那么您必须使用委托事件处理程序来附加事件。 like below 像下面

$(document).on('click','input[value="Sold Out"]',function(e){
   e.preventDefault(); //stop the event
});
 $(document).on('submit','#submitform',function(event){
        event.preventDefault();
        var input=$("#submitform :input[name='product_qty']").val();
        if(input==null)
            $(this).find('button[type=submit]').prop('disabled', true);
   }

This is what i did.It worked.However not a correct way to do this but it does the work.Stops the submit button to send any request. 这就是我所做的。它的工作原理。但是这样做不是正确的方法,但是它确实可以完成工作。停止提交按钮以发送任何请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM