我的条件检查在JS中无法正常工作

My condition checking is not properly work in JS

本文关键字:常工作 工作 检查 条件 JS 我的      更新时间:2023-09-26

当未选中复选框时,我正试图显示一条警告消息。我使用以下代码来实现这个目的

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>