这是我的代码:



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>





我有两个疑问:


当我尝试在函数外部发出警报时,clinicalVals值正在变为null
在该函数中,我得到了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

最佳答案

您每次更换clinicalVals with clinicalVals =clinicalStat + "^" + id + ":" + val;

您应该使用串联+并初始化临床值='';

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




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+"^";



                  });


    

<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>

10-07 21:09