全局变量出错

时间:2017-06-13 04:43:53

标签: javascript jquery checkbox

这是我的代码:



var clinicalStat;
var id;
var val;
var clinicalVals;

$(":checkbox").click(function() {
  //alert(" you checked");
  if ($(this).is(':checked')) {
    var checked1 = $(this).val(); //Inital value of checkbox is '0'
    alert("The inital value for selected checkbox = " + checked1);
    var checkedVal = $(this).val('1'); //value is change to '1'
    alert("The value after checked the checkbox is = " + $(this).val());
  }
});

$(":checkbox").click(function() {
  clinicalStat = document.getElementById('clinicalStat').value; //clinicalStat(type='textbox') inital value is '0'
  alert("The initial value of clinicalStat = " + clinicalStat);
  clinicalStat = document.getElementById('clinicalStat').value = "1"; //now clinicalStat value is '1'
  alert("Later the value is changed to = " + clinicalStat);
  id = (this.id);
  alert("id = " + id);
  val = (this.value);
  alert("val = " + val);
  clinicalVals = clinicalStat + "^" + id + ":" + val;
  alert("clinicalVals  = " + clinicalVals);
  //function my2();.
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-2   right2 fon">
  <h6>Clinical Practice/Procedure</h6>
  <hr>
  <p><input type="hidden" id="incidentClassifId" name="incidentClassifId" value="0"></p>
  <p><input type="hidden" id="incidentViewIndex" name="incidentViewIndex" value="0"></p>
  <p><input type="hidden" id="appendStockistStatus" value="0"></p>
  <p><input type="hidden" name="clinicalStat" id="clinicalStat" value="0"></p>
  <p><input type="checkbox" name="clinicalDoc" class="clinicalCheck" id="clinicalDoc" value="0">Documentation</p>
  <p><input type="checkbox" name="clinicalDoc" class="clinicalCheck" id="clinicalMiss" value="0">Missing Files</p>
  <p><input type="checkbox" name="clinicalDoc" class="clinicalCheck" id="clinicalPol" value="0"> Policy not available</p>
  <p><input type="checkbox" name="clinicalDoc" class="clinicalCheck" id="clinicalMed" value="0"> Medical records unavailable</p>
  <p><input type="checkbox" name="clinicalDoc" class="clinicalCheck" id="clinicalCon" value="0"> Confidentiality</p>
  <p><input type="checkbox" name="clinicalDoc" class="clinicalCheck" id="clinicalProc" value="0">Procedures not followed</p>
  <p><input type="hidden" name="clinicalVals" id="clinicalVals"></p>
  <p><input type="checkbox" id="checkBox" onclick="EnableDisableTextBox(this)"> Other (Specify)</p>
  <p><input type="text2" id="text" name="incidentClassClinicalVal" disabled="disabled" style="width: 92%"></p>
</div>
&#13;
&#13;
&#13;

我有两个疑问:

    当我尝试在该功能之外发出警报时,
  1. clinicalVals值正在null
  2. 在函数中我得到clinicalVals值。例如:如果我选择了文档复选框,我将获得clinicalVal=1^clinicalDoc:1。如果我选中了MissingFiles复选框,我将获得clinicalVal=1^clincalMiss:1。如何使用clinicalDoc clinicalVal clinicalVals=1^clincalDoc:1^1^clincalMiss:1附加此值,如果我选中所有复选框,我必须像clinicalVal一样获得最终clinicalVal=1^clincalDoc:1^1^clincalMiss:1^1^clinicalPol:1^1^clinicalMed:1^1^clinicalCon:1^1^clinicalProc:1

1 个答案:

答案 0 :(得分:0)

您每次都会替换clinicalVals with clinicalVals =clinicalStat + "^" + id + ":" + val;

您应该使用连接+并初始化clinicalVals =&#39;&#39;

clinicalVals +=clinicalStat + "^" + id + ":" + val;
            ^^^ 

&#13;
&#13;
var clinicalStat;
         var id;
         var val;
         var clinicalVals ='';

        $(":checkbox").click(function ()
               {
         //alert(" you checked");
                if($(this).is(':checked'))
                     {           
                         var checked1 = $(this).val();//Inital value of checkbox is '0'
                      

                         var checkedVal = $(this).val('1');//value is change to '1'
                        
                     }


               }); 


        $(":checkbox").click(function () 
                  {
                     clinicalStat=document.getElementById('clinicalStat').value;//clinicalStat(type='textbox') inital value is '0'
                    

                     clinicalStat=document.getElementById('clinicalStat').value="1";//now clinicalStat value is '1'
                  
                     id=(this.id);
                    
                     val=(this.value);
                   
                     clinicalVals +=clinicalStat + "^" + id + ":" + val+"^";
                    


                  });
                  
                  
    
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-2   right2 fon">
    <h6>Clinical Practice/Procedure</h6>
        <hr>
            <p><input type="hidden" id="incidentClassifId" name="incidentClassifId" value="0"></p>
            <p><input type="hidden" id="incidentViewIndex" name="incidentViewIndex" value="0"></p>
            <p><input type="hidden" id="appendStockistStatus" value="0"></p>
            <p><input type="hidden" name="clinicalStat" id="clinicalStat" value="0"></p>
            <p><input type="checkbox" name="clinicalDoc" class="clinicalCheck" id="clinicalDoc" value="0">Documentation</p>
            <p><input type="checkbox" name="clinicalDoc" class="clinicalCheck" id="clinicalMiss" value="0">Missing Files</p>
            <p><input type="checkbox" name="clinicalDoc" class="clinicalCheck" id="clinicalPol" value="0" > Policy not available</p>
            <p><input type="checkbox" name="clinicalDoc" class="clinicalCheck" id="clinicalMed" value="0" > Medical records unavailable</p>
            <p><input type="checkbox" name="clinicalDoc" class="clinicalCheck" id="clinicalCon" value="0" > Confidentiality</p>
            <p><input type="checkbox" name="clinicalDoc" class="clinicalCheck" id="clinicalProc" value="0">Procedures not followed</p>
            <p><input type="hidden" name="clinicalVals" id="clinicalVals" ></p>
            <p><input type="checkbox" id="checkBox" onclick="EnableDisableTextBox(this)" > Other (Specify)</p>
            <p><input type="text2" id="text" name="incidentClassClinicalVal"  disabled="disabled" style="width: 92%"></p>
    </div>
    <button onclick ="alert(clinicalVals)">Show</button>
&#13;
&#13;
&#13;