【发布时间】:2015-11-17 07:15: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: 无法读取 null 的属性“样式”
我想限制用户输入某些内容,我已经在输入字段中应用了 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';
【问题讨论】:
-
您不会在提供的代码中的任何位置读/写
style属性。 -
我认为错误在另一个地方。因为在您的代码中您没有访问
Style属性 -
我没有,我只是获取用户通过 getelementsbyname 输入的值
-
你能发布函数
onclick_url_bid_o_matic的代码 -
@wonderbell 刚刚做了
标签: javascript html validation