检查数组中是否存在值不起作用

时间:2015-04-20 20:08:48

标签: javascript jquery

我有以下脚本在激活按钮并根据某些限制选择选项时工作正常。

<script>
var update_selectopt=function(){
$("tr").each(function(){
  if($("input[type='checkbox']", this).is(":checked")){
    $('#select',this).removeAttr('disabled');
  }
  else{
    $('#select',this).attr('disabled','disabled');
  }
});
};

function updateSubmitButtonState(){

var enableableLineCount= 0;
$("tr").each(function(){

if( isEnableableLine( this ) )
  enableableLineCount++;
});

if( enableableLineCount > 0 )
   $('[type="submit"]').removeAttr("disabled");
else
   $('[type="submit"]').attr("disabled","disabled");


function isEnableableLine( tr ){

if( 
    $("input[type='checkbox']", tr).is(":checked") && 
    $("select option[value='no_action']:selected", tr ).length == 0 
)
  return true;
else
  return false;
  }
}

$(update_selectopt);
$("input[type='checkbox']").on("click",update_selectopt);

$("input[type='checkbox']").on("click",updateSubmitButtonState );
$("select").on("change",updateSubmitButtonState );
</script>

但是,当我添加用于检查数组中的值的代码并弹出警告框时,如果没有,则它不起作用,也会破坏代码 - 使按钮始终处于非活动状态,选择选项始终处于活动状态。此外,我很困惑在哪里放置代码以保持按钮始终处于非活动状态,直到所有条件都不成立为止。

完整的脚本

<script>

var state_list_swap=["AVAILABLE","IMPAIRED"]
var state_list_shut=["ERROR"]
var state_list_scan=["SUSPENDED"]

var update_selectopt=function(){
$("tr").each(function(){
  if($("input[type='checkbox']", this).is(":checked")){
    $('#select',this).removeAttr('disabled');
  }
  else{
    $('#select',this).attr('disabled','disabled');
  }
});
};

function updateSubmitButtonState(){

var enableableLineCount= 0;
$("tr").each(function(){

if( isEnableableLine( this ) )
  enableableLineCount++;
});

if( enableableLineCount > 0 )
   $('[type="submit"]').removeAttr("disabled");
else
   $('[type="submit"]').attr("disabled","disabled");


function isEnableableLine( tr ){

if( 
    $("input[type='checkbox']", tr).is(":checked") && 
    $("select option[value='no_action']:selected", tr ).length == 0 
)
  return true;
else
  return false;
 }
}

$(update_selectopt);
$("input[type='checkbox']").on("click",update_selectopt);

$("input[type='checkbox']").on("click",updateSubmitButtonState );
$("select").on("change",updateSubmitButtonState );


$("input[type='submit']").click(function(){
 $("tr").each(function(){
  if (
    $("select option[value='swap']:selected",tr)
    ){
        var cells=$(this).find('td');
        $(cells).each(function(){
            if (jQuery.inArray($(this).text(),state_list_swap)!=-1){
              alert('swap is allowed only with "AVAILABLE","IMPAIRED" states');
            } 
          });
    }
    else if(
        $("select option[value='scan']:selected",tr)
      ){
          var cells=$(this).find('td');
          $(cells).each(function(){
              if (jQuery.inArray($(this).text(),state_list_scan)!=-1){
                alert('scan is allowed only with "SUSPENDED');
              }  
            });

        } 
    else if(
        $("select option[value='shut']:selected",tr)
      ){
          var cells=$(this).find('td');
          $(cells).each(function(){
              if (jQuery.inArray($(this).text(),state_list_shut)!=-1){
                alert('shut is allowed only with "ERROR" states');
              }  
            });

        } 

  }    

});

</script>

1 个答案:

答案 0 :(得分:0)

尝试以下修正:

<script>

    var state_list_swap=["AVAILABLE","IMPAIRED"]
    var state_list_shut=["ERROR"]
    var state_list_scan=["SUSPENDED"]

    var update_selectopt=function(){
        $("tr").each(function(){
            if($("input[type='checkbox']", this).is(":checked")){
                $('#select',this).removeAttr('disabled');
            }
            else{
                $('#select',this).attr('disabled','disabled');
            }
        });
    };

    function updateSubmitButtonState(){

        var enableableLineCount= 0;
        $("tr").each(function(){
            if( isEnableableLine( this ) ){
                enableableLineCount++;
            }
        });

        if( enableableLineCount > 0 ){
            $('[type="submit"]').removeAttr("disabled");
        }else{
            $('[type="submit"]').attr("disabled","disabled");
        }

        function isEnableableLine( tr ){
            if( 
                $("input[type='checkbox']", tr).is(":checked") && 
                $("select option[value='no_action']:selected", tr ).length == 0 
            ){
                return true;
            }else{ 
                return false;
            }
        }
    }
    //WHATS THIS 
    //$(update_selectopt);

    $("input[type='checkbox']").on("click", update_selectopt);

    $("input[type='checkbox']").on("click", updateSubmitButtonState );

    $("select").on("change",updateSubmitButtonState );


    $("input[type='submit']").click(function(){
        //  inside this, you had no reference to what 'tr' was
        //  --added it as the element reference in the .each loop
        $("tr").each(function (ind, tr) {
            if ($("select option[value='swap']:selected", tr)) {
                var cells = $(this).find('td');
                $(cells).each(function (index, element) {
                    if (jQuery.inArray($(element).text(), state_list_swap) != -1) {
                        alert('swap is allowed only with "AVAILABLE","IMPAIRED" states');
                    }
                });
            }
            else if (
                $("select option[value='scan']:selected", tr)
              ) {
                var cells = $(this).find('td');
                $(cells).each(function (index, element) {
                    if (jQuery.inArray($(element).text(), state_list_scan) != -1) {
                        alert('scan is allowed only with "SUSPENDED');
                    }
                });

            }
            else if (
                $("select option[value='shut']:selected", tr)
              ) {
                var cells = $(this).find('td');
                $(cells).each(function (index, element) {
                    if (jQuery.inArray($(element).text(), state_list_shut) != -1) {
                        alert('shut is allowed only with "ERROR" states');
                    }
                });

            }
        });
    });

</script>