我的条件检查在JS中无法正常工作
My condition checking is not properly work in JS
当未选中复选框时,我正试图显示一条警告消息。我使用以下代码来实现这个目的
function IsEmpty(){
var oldpath = document.forms['pathuploader'].oldpath.value;
var newpath = document.forms['pathuploader'].newpath.value;
var metavalue = !document.forms['pathuploader'].chkmeta.checked;
var postvalue = !document.forms['pathuploader'].chkpost.checked;
if((oldpath == "")||((oldpath.substring(0,4))!='http')||((oldpath.substring(0,4))=='Http'))
{
alert("Enter a valid URL");
return false;
}
if((newpath == "")||(newpath.substring(0,4)!='http')||(newath.substring(0,4)!='Http'))
{
alert("Enter a valid URL");
return false;
}
if((metavalue) && (postvalue))
{
alert("Select any category to change");
return false;
}
return true;
}
工作JSFiddle
首先,你有一个打字错误在下面的行
if((newpath == "")||(newpath.substring(0,4)!='http')||(newath.substring(0,4)!='Http'))
最后一个if是"newath",应该是"newpath",同一区域"!="应该与旧路径逻辑匹配,而应该是"=="。
若要进一步清理代码,请使用"==="answers"!===",而不是仅使用"==",因为这样可以进行更精确的比较。
查看此链接了解更多信息使用严格模式
这是调整后的代码
此外,如果您希望遵守JS标准,请尝试使用camelBase命名约定。作为一个例子,我已经将"IsEmpty"函数更正为"IsEmpty"。
function isEmpty(){
var oldpath = document.forms['pathuploader'].oldpath.value;
var newpath = document.forms['pathuploader'].newpath.value;
var metavalue = !document.forms['pathuploader'].chkmeta.checked;
var postvalue = !document.forms['pathuploader'].chkpost.checked;
if((oldpath === "")||((oldpath.substring(0,4))!=='http')||((oldpath.substring(0,4))==='Http'))
{
alert("Enter a valid old URL");
return false;
}
if((newpath === "")||(newpath.substring(0,4)!=='http')||(newpath.substring(0,4)==='Http')){
alert("Enter a valid new URL");
return false;
}
if((metavalue) && (postvalue)){
alert("Select any category to change");
return false;
}
return true;
}
更新我也同意BANG(!)应该在哪里的"Sourabh"
if(( !metavalue ) && ( !postvalue ){
而不是目前的情况。两者都有效,但是BANG隐藏在变量中。如果你把它放在原处,也许你可以通过调用来提醒下一个可能查看你代码的程序员
var metaValueNotChecked = !document.forms...
var postValueNotChecked = !document.forms...
然后它将正确读取为
if(( metaValueNotChecked ) && ( postValueNotChecked ){
在这种情况下,BANG应该在你有它的地方。
希望这能有所帮助!
使用下面的过程来获得更好的方法,我假设您在表单中定义了元素,您需要更改代码的这两部分
第一个:
var metavalue = document.forms['pathuploader'].chkmeta.checked;
var postvalue = document.forms['pathuploader'].chkpost.checked;
则在if条件下使用以下程序:
if(!metavalue && !postvalue)
{
alert("Select any category to change");
return false;
}
您可以使用HTML5中的"required",并在选中复选框后将其从其他复选框中删除。例如:
<input required="required" value="1" name="site[source][]" id="site_source_any" type="checkbox">
<input required="required" value="2" name="site[source][]" id="site_source_many" type="checkbox">
在您的脚本文件中:
<script type="text/javascript">
// Check if atleast one of the checkbox is checked
$(function(){
var requiredCheckboxes = $(':checkbox[required]');
requiredCheckboxes.change(function(){
if(requiredCheckboxes.is(':checked')) {
// Remove Required once at-least one is checked
requiredCheckboxes.removeAttr('required');
}
else {
requiredCheckboxes.attr('required', 'required');
}
});
});
</script>
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- jQuery UI自动完成突然停止工作
- AngularJS UI路由器不能像ng路由器那样工作
- HTML5音频加载和播放获胜'我不能在iPad上工作
- JavaScript打印功能使日历停止工作
- Javascript.getHours()工作不正常
- 为什么这在IE中的工作方式与在Firefox中不同
- 视频HTML没有'无法在Internet Explorer 11上工作
- 扩展移相器按钮类不工作
- Firebase迁移-简单的Firebase.set没有'不再工作了——旧的还是新的
- 谷歌地图不是以HTML显示,而是在JS Fiddle上工作
- 正在尝试使用if和else添加类,但无法正常工作
- Jquery FadeIn FadeOut 只工作一次
- Foreach无法在Typescript中工作
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- JS可以在Chrome中工作,但不能在Firefox中工作
- ajaxToolkit PopupControlExtender不工作.过时的
- HTML标记在脚本标记中工作
- javascript扫雷器floodfill算法不能正常工作