用于输入检查的 JavaScript 函数不起作用
javascript function for input check not working
我有javascript函数,它正在检查某些输入字段是否为空或选中某些复选框等。
function empty() {
//NAME CHECK
var x;
x = document.getElementById("required").value;
if (x == "") {
alert("Name is empty!");
return false;
};
//CATEGORY CHECK
var selectedCat = document.getElementById('parent_cat');
if (selectedCat.selectedIndex.value != null) {
alert("please, select category!");
return false;
};
//price check
var priceBG;
priceBG = document.getElementById("requiredPriceBG").value;
if (priceBG == "") {
alert("BG price is sempty!");
return false;
};
var priceEN;
priceEN = document.getElementById("requiredPriceEN").value;
if (priceEN == "") {
alert("En price is empty!");
return false;
};
//Checkboxes check
var checkboxs = document.getElementsByName("amentities");
var okay = false;
for(var i = 0, l = checkboxs.length; i < l; i++){
if(checkboxs[i].checked){
okay = true;
break;
};
};
if(!okay){
alert("At least one checkbox should be checked!");
return false;
};
};
第一次检查(名称检查)后没有任何工作。名称检查有效,然后提交表单,而不考虑其他检查。
据我所知,html代码是正确的,并且没有重复的ID。
可能是什么问题,有没有办法优化功能?
以下是对代码的更正:
代码笔链接
正如Ursus所说,它被选中了Cat.value。这是你的代码:
function empty() {
//NAME CHECK
var x;
x = document.getElementById("required").value;
if (x == "") {
alert("Name is empty!");
return false;
};
//CATEGORY CHECK
var selectedCat = document.getElementById('parent_cat');
if (selectedCat.value != null && selectedCat.value == "") {
alert("please, select category!");
return false;
};
//price check
var priceBG;
priceBG = document.getElementById("requiredPriceBG").value;
if (priceBG == "") {
alert("BG price is sempty!");
return false;
};
var priceEN;
priceEN = document.getElementById("requiredPriceEN").value;
if (priceEN == "") {
alert("En price is empty!");
return false;
};
//Checkboxes check
var checkboxs = document.getElementsByName("amentities");
var okay = false;
for(var i = 0, l = checkboxs.length; i < l; i++){
if(checkboxs[i].checked){
okay = true;
break;
};
};
if(!okay){
alert("At least one checkbox should be checked!");
return false;
};
};
selectedCat.selectedIndex.value
不应该selectedCat.value
?另外,请检查控制台中是否有一些错误。
相关文章:
- Ember Data DS.Model's set函数不起作用
- 为什么我的JavaScript堆栈排序函数不起作用
- Javascript onchange()函数不起作用
- 另一个Ajax函数触发的Ajax函数不起作用
- 从PHP调用JS函数不起作用
- jQuery validate函数不起作用
- JS-窗口宽度函数不起作用
- javascript函数,该函数不起作用,但不会显示任何错误
- 为什么这个排序函数不起作用
- 为什么这个反向函数不起作用
- Javascript parseInt 函数不起作用
- 我的函数不起作用,Ajax调用,JQuery,调用外部?我不知道
- 主干构造函数不起作用
- Javascript:this.value 函数不起作用
- Mocha 的 beforeEach() 和 done() 函数不起作用
- 在jQuery中,我的函数不起作用
- jquery-ui-rails的draggable和dropable函数不起作用
- 数学函数不起作用
- Javascript-创建打印到HTML的函数不起作用
- 我上传了客户's服务器一个js函数不起作用