Javascript null输入错误

Javascript null input error

本文关键字:错误 输入 null Javascript      更新时间:2023-09-26

我有这个输入字段

  html +='<p style="font-size: 18px; ">Total Bids: <input type="text" class="total_bids" name="total_bids" placeholder="No. of Bids" required></p>';

和按钮

html += '<a style="cursor:pointer;" onclick="'+onclick_url_bid_o_matic+'" class="btn-bidoo" id="btn-bidoo">Activate</a>';

我通过获取输入字段的值

var totalbids = document.getElementsByName('total_bids')[0].value;

如果用户输入了一个数字,它可以正常工作,但如果用户没有输入任何内容并按下激活按钮,它会给出这个错误,

TypeError:无法读取空的属性"style"

我想限制用户输入一些东西,我已经在输入字段中应用了required,但这不起作用,(我不知道为什么)我可以限制用户输入任何其他检查?

编辑:

我在php中得到了这个值,我也在那里应用了一个检查,

 $total_bids = PSF::requestGetPOST('totalbids');
if(!$total_bids)
        {
            return json_encode(array('error' => 'enter a number'));
        }

但它直到这里才到达,因为它以空值错误停止

该按钮指向该功能

bid_o_matic:function(id, uid, e, evt){
            var totalbids = document.getElementsByName('total_bids')[0].value;
            var bidval = document.getElementsByName('bidvalue')[0].value;

            parent_element = $(e).parent().parent();

            if(typeof(evt) != 'undefined'){
                evt.preventDefault();
                evt.stopPropagation();
            }
            if(e.attr('disabled') == 'disabled'){
                return;
            }
            e.button('loading');
            $.post('index.php', {
                page:'__request',
                module:'PSF_auctions',
                action:'make_bid_o_matic',
                id:id,
                uid:uid,
                totalbids: totalbids,
                bidval: bidval
            }, function(d){
                e.button('reset');
                document.getElementById("btn-bidoo").style.visibility = 'hidden';
                document.getElementById("btn-deactivate").style.visibility = 'visible';
                document.getElementById("totalbids").style.visibility = 'hidden';
                document.getElementById("bidvalue").style.visibility = 'hidden';
                if(d.error){
                //        document.getElementById("totalbids").style.visibility = 'visible';
                // document.getElementById("bidvalue").style.visibility = 'visible';
                     $.auctions.alert(d.error);
                    document.getElementById("btn-bidoo").style.visibility = 'visible';
                    document.getElementById("btn-deactivate").style.visibility = 'hidden';
                }
                else
                {
              $(current_price_element).html('<p style="color:#f47321; font-size:16px; font-weight:bold;">Current Price</p><a href="#">'+d.bid_value+'</a>')
                }
            }, 'json');
        },

并且错误发生在该行中

 document.getElementById("totalbids").style.visibility = 'hidden';
发生这种情况是因为total_bids是输入的class而不是id。您没有任何名为totalbidsid。当你试图找到一个像totalbids一样具有id的对象时,你却一无所获。要解决它,请像这样更改您的输入。

<input type="text" id="totalbids" class="total_bids" name="total_bids" placeholder="No. of Bids" required></p>

或使用

document.getElementsByName('total_bids')[0].style.visibility = 'hidden'

因为你做这件事的方式不对。您实际上是通过使用HYPERLINK 将表格张贴到服务器端

html += '<a style="cursor:pointer;" onclick="'+onclick_url_bid_o_matic+'" class="btn-bidoo" id="btn-bidoo">Activate</a>';

这是提交表单的错误方式。请将其替换为实际输入的提交按钮。然后它将验证您的文本字段

    html += '<input type="submit" class="btn-bidoo" id="btn-bidoo" value="Activate" />';

并在表单标签上设置onclick_url_bid_o_maticurl

html += '<form action="' + onclick_url_bid_o_matic + '" method="post">'

或者,如果你仍然想使用超链接标签,那么在Javascript/Jquery 的帮助下提交你的表格

这就是为什么您在请求的URL

中得到NULL