2013-12-16 55 views
-2

我想要放入文本區域由JavaScript,一旦選中,它將從陣列中彈出...這裏我所有的複選框值的代碼:獲取從選中的複選框全部價值,並投入一個textarea

<form enctype="multipart/form-data" method="get" id="frmMain" name="frmMain"> 
    <input type = "checkbox" id = "chk1" value = "CheckBox1" onclick ='checkTickArea(this.id)'> 
    <input type = "checkbox" id = "chk2" value = "CheckBox2" onclick = 'checkTickArea(this.id)'> 
    <input type = "checkbox" id = "chk3" value = "CheckBox3" onclick = 'checkTickArea(this.id)'> 
    <input type = "checkbox" id = "chk4" value = "CheckBox4" onclick = 'checkTickArea(this.id)'> 
    <input type = "checkbox" id = "chk5" value = "CheckBox5" onclick = 'checkTickArea(this.id)'> 
    <textarea id= "taSignOff" style="width:180px;height:90px;font-size:20px; resize: none;" rows="3" readonly> </textarea> 
</form> 

的javascript:

var selectedvalue=[]; 
function checkTickArea(id) 
    { 

     $('#'+id).change(function() { 
      //If checked then push the value 
      if($('#'+id).is(":checked")) 
      { 
       selectedvalue.push($('#'+id).attr("value")); 
      }else 
      { 
       //This is what pops the value from the array when the checkbox is unchecked. 
      /* var index = selectedvalue.indexOf($('#'+id).attr("value")); 
       if (index > -1) { 
        selectedvalue.splice(index, 1); 
       }*/ 
       selectedvalue.splice(selectedvalue.indexOf($(this).attr("value")),1); 
      } 
      document.getElementById('taSignOff').value = selectedvalue.join("->\n"); 
     }); 
    } 

此外,該功能只適用於Firefox ...但在Chrome和IE瀏覽器,它不工作...

+1

...好哪來的'checkTickArea'功能? – Doorknob

+0

告訴我們您嘗試過的JavaScript – hammus

+0

對不起,我忘了加...你去那裏,我編輯的問題,現在... @DoorknobofSnow – k3vin023

回答

0

這對我的作品,雖然我不知道你爲什麼不這樣做jQuery的本身的textarea的:jsFiddle

HTML:

<form enctype="multipart/form-data" method="get" id="frmMain" name="frmMain"> 
    <input type = "checkbox" id = "chk1" value = "CheckBox1" /> 
    <input type = "checkbox" id = "chk2" value = "CheckBox2" /> 
    <input type = "checkbox" id = "chk3" value = "CheckBox3" /> 
    <input type = "checkbox" id = "chk4" value = "CheckBox4" /> 
    <input type = "checkbox" id = "chk5" value = "CheckBox5" /> 
    <textarea id= "taSignOff" style="width:180px;height:90px;font-size:20px; resize: none;" rows="3" readonly> </textarea> 
</form> 

的JavaScript:

var selectedvalue = []; 

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

function checkTickArea() { 

    //If checked then push the value 
    if ($(this).is(":checked")) { 
     selectedvalue.push($(this).val()); 
    } else { 
     selectedvalue.splice(selectedvalue.indexOf($(this).val()), 1); 
    } 

    // Why not use jQuery tho? 
    //$('taSignOff').val(selectedvalue.join("->\n")); 
    document.getElementById('taSignOff').value = selectedvalue.join("->\n"); 
} 
0

刪除內聯JavaScript和做:

var elems = $('[id^="chk"]'); 

elems.on('change', function() { 
    $('#taSignOff').val(
     elems.filter(':checked').map(function() { 
      return this.value; 
     }).get().join("->\n") 
    ); 
}); 

FIDDLE

相關問題