2015-04-07 74 views
0

我有一些复选框和一个文本区域,并收集文本区域中的所有复选框值,然后插入它在数据库中,当我从数据库中选择数据时,我只有文本区域的数据而不是复选框。我想要的东西是:当我查看它们时,必须检查之前检查过的复选框。现在我可以如何检查复选框的值是否存在于文本区域,然后选中该复选框?我的代码在这里?如何检查复选框,如果它的值存在于文本区域

     <div style="margin-top:50px;"> 
          <div style="margin-top:-30px;"> 
           <h4>Header Options</h4><br> 
           <table style="width:70%;float:left"> 
            <tbody> 
             <tr> 
              <td><input type="checkbox" id="h_option1" name="h_option1" value="Company Name"> Company Name </td> 
              <td><input type="checkbox" id="h_option2" name="h_option2" value="Company Address"> Company Address</td> 
              <td><input type="checkbox" id="h_option3" name="h_option3" value="Month"> Month</td> 
              <td> <input type="checkbox" id="h_option4" name="h_option4" value="Name"> Name </td> 
             </tr> 
             <tr> 
              <td> <input type="checkbox" id="h_option5" name="h_option5" value="Employee Ref No"> Employee Ref No </td> 
              <td><input type="checkbox" id="h_option6" name="h_option6" value="Designation"> Designation </td> 
              <td><input type="checkbox" id="h_option7" name="h_option7" value="Date of joining"> Date of joining </td> 
             </tr> 
            </tbody> 
           </table> 
           <div> 
            <span> 
             <textarea readonly="" type="text" id="header_options" name="header_options" rows="5" cols="25">Company Name,Company Address,Month,Employee Ref No,</textarea> 
            </span> 
           </div> 
          </div> 
          <br> 
          <br> 
          <div align="center"> 
           <input type="button" value="Configure" class="detailsbutton" onclick="get_check_value();"> 
          </div> 
          <br> 
         </div> 

这是我希望得到的结果是: the expected result ,但它给了我下面的结果: the current result

注:我复选框的每个值分开与文本区域内一个逗号。

+0

你能提供的功能* get_check_value()*的代码?问题应该在这里。 – Tolokoban

回答

2
var options = document.getElementById('header_options').value; 
options = options.split(','); 
for(var i = 0; i < options.length; i++){ 
//Set "checked" for element where the value is the current value in options array 
    if(options[i]) document.querySelectorAll('input[value="'+options[i]+'"][type="checkbox"]')[0].checked = true; 
} 

Fiddle

1

首先,你必须创建一个存在于textarea中的所有复选框的数组,然后你必须使用复选框的id来检查相应数组中存在哪些复选框值。然后根据isExist函数的结果设置选中和取消选中

0

虽然您可以在服务器端代码(PHP,JSP或任何您使用的技术)上做得更好,但是如果您真的想在客户端执行此操作,那么这是纯JavaScript中的一种方式(适用于几乎所有现代浏览器版本)。

创建一个功能选择复选框,

<script type="text/javascript"> 
function selectChkbox() { 
    var strAllValues = document.getElementById('header_options').value; // Contains all values 
    var chkboxArr = document.getElementsByTagName('input'); 
    for(var i=0; i<chkboxArr.length; i++) { 
    if(chkboxArr[i].type == 'checkbox') { 
     if(strAllValues.indexOf(chkboxArr[i]) != -1) { // check if checkbox value is present in all values 
      chkboxArr[i].checked = true; 
     } 
    } 
    } 
} 
</script> 

调用此函数页面加载时。就像这样:

<body onload="selectChkbox();"> 

希望它能帮助,谢谢。

相关问题