简体   繁体   English

复选框-选中添加文本,取消选中删除文本区域中的原始文本

[英]Checkbox - check add text, uncheck remove original text in text area

<script language="javascript" type="text/javascript">

function moveNumbers(num) { 
   var txt=document.getElementById("result").value; 
   txt=txt + num; 
   document.getElementById("result").value=txt; 
} 
</script>

<textarea id="result" name="image_id" rows="8" cols="11" readonly>

</textarea>
<tr>

    <?php
        $path = "photos/";
        $dir_handle = @opendir($path) or die("Unable to open folder");
        echo "<table height='500px'width='800px'align='center'border='1'>";
        echo "<tr>";
        while (false !== ($file = readdir($dir_handle))) {

          if($file == "index.php")
          continue;
          if($file == ".")
          continue;
          if($file == "..")
          continue;

          echo ($x % 6 == 0) ? "</tr><tr>" : "";
          echo "<td><input type='checkbox' name='add' value='$file'             
          onclick='moveNumbers(this.value)'>
          <img src='photos/$file'alt='$file' style='height:auto;width:50%;'alt='$file'>
          <br>
          $file
          </td>";
          $x++;
       }
       echo "</tr>";
       echo "</table>";
       closedir($dir_handle);
   ?>

Hi all, having some trouble with the check boxes. 大家好,复选框有些麻烦。 Click on the check box, text appears in text area no problem. 单击复选框,文本出现在文本区域中没问题。 I have been trying to figure out how to when you uncheck the check box the text is removed. 我一直在尝试找出如何取消选中该复选框时将文本删除。 EG Checked -> Text 123 inputted, Uncheck -> Test 123 removed. EG已检查->输入文本123,取消选中->测试123已删除。 Cheers. 干杯。

So you want different activity depending on whether your text box is being checked or unchecked. 因此,您希望根据文本框是处于选中状态还是未选中状态进行不同的活动。 At the moment, you call one method whenever you click on the checkbox, so now you need to look at the state of the checkbox, when it is clicked on. 此刻,只要您单击复选框,便会调用一种方法,因此,现在需要查看单击复选框时的状态。 Give your checkbox an ID, and swap out your Javascript for this. 给您的复选框一个ID,并为此交换出Javascript。

<script language="javascript" type="text/javascript">

function moveNumbers(num) { 
 if(document.getElementById("checkBoxId").checked){
  var txt=document.getElementById("result").value; 
  txt=txt + num; 
  document.getElementById("result").value=txt; 
 }
 else{
 document.getElementById("result").value=txt="";
 }
} 

</script>

just add jquery and this will help you.. 只需添加jquery,这将对您有所帮助。

<script type='text/javascript'>        
$(document).ready(function(){
    $('#textCheck').change(function(){
        if($('#textCheck').attr('checked'))
        {
            $('#myTextArea').val($('#textinput').val());
        }
        else
        {
            $('#myTextArea').val('');
        }
    });
});
</script>
<input type = "text" name = 'textinput' id = 'textinput' />
<textarea name = 'myTextArea' id = 'myTextArea'></textarea>
<input type="checkbox" name='textChck' id='textCheck' />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM