【问题标题】:Clear Text Box on Selection and Delete Button appear on Selection选择上的清除文本框和删除按钮出现在选择上
【发布时间】:2015-05-11 09:21:26
【问题描述】:

我有一个用户用来将信息保存到文本文件的表单,然后是一个下拉列表,用于拉出他们的文件名并将该信息显示回文本字段,但是我正在尝试弄清楚如何才能选择一个值时清除文本字段。除了我的 php 之外,我还有一个 (Add New Code) ,它只是一个占位符,因此他们可以使用表单来保存数据。我想弄清楚的是如何在他们选择特定下拉菜单时清除文本框。我还想添加一个删除按钮,只要从下拉列表中选择文件,就会出现该按钮。下面是我的相关编码。

感谢您对显示问题和 css 按钮的帮助现在正在尝试找出删除按钮的 php 脚本以删除当前选定的文件。

<input type="hidden" name="Action" value="EDIT" /><input type="hidden" name="Selection"  id="Selection" value="-1"><div>Below is the list of your saved codes. To edit your codes, select it from the list.</div>
<select size="1" name="CodeList" id="CodeList" onchange="CodeChange();"><option value="0">(Add New Code)</option>
<?php
    $directory = $directory = 'users/' . $_SESSION['username'];
    $filesContents = Array();
    $files = scandir( $directory ) ;

     foreach( $files as $file )
    {
   if ( ! is_dir( $file ) )
  {
 $filesContents[$file] = file_get_contents($directory , $file);
echo "<option>" . $file . "</option>";
}
}
?>
</select>
        <h3>Saved Codes</h3>
        <form method="post" action="/evo/avsaveprocess.php">
            <input type="hidden" name="Action" value="SAVE" />
            <input type="hidden" name="CodeId" id="CodeId" value="0" />
            <table width="100%" border="0">
                <tr>
                    <td>Description:</td>
                    <td><input type="text" name="CodeDescription" size="40" maxlength="50" id="CodeName" value="" /></td>
                </tr>
                <tr>
                    <td valign="top">Code:</td>
                    <td>
                        <textarea rows="10" style="width:99%" name="Code" id="CodeValue"></textarea>
                    </td>
                </tr>
            </table>
            <input type="submit" value="Save" />
            </form>
<script>
    $(document).ready(function(){
      // apply a change event
       $('#CodeList').change(function() {
         // update input box with the currently selected value
         $('#CodeName').val($(this).val());
         $.get( '<? echo $directory ?>' + '/' + $('#CodeName').val(), function( data ) {
        $( "#CodeValue" ).text( data );
    });
  });
 });

【问题讨论】:

    标签: php jquery html


    【解决方案1】:
    < option value="0" >(Add New Code)< /option >
    

    我不明白value="0"的目的

    选择&lt;option&gt;&lt;select id="id"&gt;

    检索.selectedIndex

    然后您使用selectedIndex 访问带有.options[selectedIndex].text 的选定选项

    我不会做jQuery,但你可以轻松翻译。

    我假设您想清除或更改选项文本。

    这是在选择选项中清除文本的代码:
    将文本添加到空字符串以更改文本值。

    var ndx = document.getElementById('id').selectedIndex;
    
    document.getElementById('id').options[ndx].text='';
    

    jQuery(不是我的)

    (来自get index of selected option with jQuery

    $("#id option:selected").index()
    

    (来自jQuery get specific option tag text

    $("#id[ndx]").text();
    

    删除按钮 HTML:

    <button  type="button" id="b1" class="hide">Delete</button>
    

    删除按钮 CSS:

    .hide{display:none;}
    

    删除按钮 JS

    document.getElementById('b1').style.display='block';
    

    成功了:

    <!DOCTYPE html>
    <html lang="en"><head><title>Unhide Delete</title>
    <style type="text/css">
    .hide{display:none;}
    </style></head><body>
    <button  type="button" id="b1" class="hide">Delete</button>
    <script type="text/javascript">
    //<![CDATA[
    document.getElementById('b1').style.display='block';
    //]]>
    </script></body></html>
    

    这个切换删除按钮

    <!DOCTYPE html>
    <html lang="en"><head><title>Unhide Delete</title>
    <style type="text/css">
    .hide{display:none;}
    </style></head><body>
    <button  type="button"  onclick="hideShow()">Show Hide Delete</button><br/> <br/>
    <button  type="button" id="b1" class="hide">Delete</button>
    <script type="text/javascript">
    //<![CDATA[
    var toggle = new Array();
    toggle['none'] = 'block';
    toggle['block'] = 'none';
    var del = document.getElementById('b1');
    del.style.display='block';
    function hideShow(){
    del.style.display=toggle[del.style.display];
    }
    //]]>
    </script></body></html>
    

    我的页面版本:

    我过滤了文件以仅包含扩展名为 .php 的文件以供我测试
    if(pathinfo($file,PATHINFO_EXTENSION) == 'php' ){
    问题和解决方案:
    文件内容无法存储在变量中。
    将换行符转换为
    preg_replace('/\n/','
    ',$contents);
    将 &lt; preg_replace('/</','&lt;',$contents);
    已转换 > 为 &amp;gt; preg_replace('/&gt;/','&amp;gt;',$contents);');
    然后在将文本设置为内容数组的内容时,必须将 >、> 和
    转换回它们的原始字符。
    var temp = contents[ndx].replace(/&amp;lt;/g,'&lt;');
    temp = temp.replace(/&amp;gt;/g,'&gt;');
    txt.value= temp.replace(/&lt;br&gt;/g,"\\n");
    需要调整 textarea 的大小以显示内容
    添加滚动条
    CSS:overflow:scroll;
    设置为内容高度
    txt.style.height= txt.scrollHeight;
    当内容非常大时 textarea 太大。
    有限的文本区域高度以适合浏览器窗口。
    var maxHeight = (window.innerHeight - txt.offsetTop) - 40;
    var h = txt.scrollHeight;
    if(h &gt; maxHeight){h = maxHeight;}
    txt.style.height = h + 'px';
    + 'px' 很重要
    当小文件跟随大文件时,文本区域高度必须降低
    txt.style.height = '100px';
    第一个选项需要留空,否则第一个选项不容易被选中,因为不会有更改事件。
    将第一个数组元素设为空白
    \ncontents[0] = '';\n
    设置 $ndx = 1 而不是零
    file_get_contents 需要的文件目录,而不向选项添加目录
    将逗号改为点
    file_get_contents($directory , $file);
    file_get_contents($directory . $file);

    现在我对这个页面相当满意:

    <?php ob_start("ob_gzhandler");
    header('Content-Type: text/html; charset=utf-8');
    header('Connection: Keep-Alive');
    header('Keep-Alive: timeout=5, max=100');
    header('Cache-Control: max-age=120');
    echo <<<EOT
    <!DOCTYPE html>
    <html lang="en"><head><title>Code</title>
    <style type="text/css">
    #CodeValue{width:50%;background:#eff; width:80%;font:400 1em "Courier New", Courier, monospace;overflow:scroll;} 
    .btn{width:50%;margin:0 0 .5em 0;border-radius: 3px 3px 3px 3px;font: 700 1.2em Arial,Helvetica,Calibri,sans-serif;overflow: visible;border:1px solid #00f;color: #fff;padding: .1em;
    background-image: -o-linear-gradient(bottom, #2ef 0%, #02f 100%);
    background-image: -moz-linear-gradient(bottom, #2ef 0%, #02f 100%);
    background-image: -webkit-linear-gradient(bottom, #2ef 0%, #02f 100%);
    background-image: -ms-linear-gradient(bottom, #2ef 0%, #02f 100%);
    background-image: linear-gradient(to bottom, #2ef 0%, #02f 100%);}
    </style>
    </head><body>
    EOT;
    ob_flush();
    $ndx = 1;
    $js = "var contents=new Array();\ncontents[0] = '';\n";
    $directory = 'users/';
    $files = scandir($directory) ;
    $options = "<option></option>\n";
    foreach( $files as $file ){
      if ( !is_dir($file) ){
    
        if(pathinfo($file,PATHINFO_EXTENSION) == 'php' ){
          $options .= "<option>$file</option>\n";
          $contents = file_get_contents($directory . $file);
          $contents = preg_replace('/</','&lt;',$contents);
          $contents = preg_replace('/>/','&gt;',$contents);
          $contents = preg_replace('/\n/','<br>',$contents);
          $contents = addslashes( $contents);
          $js .= "contents[$ndx] = \"$contents\"\n";
          $ndx++;
        }
      }
    }
    echo <<<EOT
    <input type="hidden" name="Action" value="EDIT" /><input type="hidden" name="Selection"  id="Selection" value="-1"><div>Below is the list of your saved codes. To edit your codes, select it from the list.</div>
    <select size="1" name="CodeList" id="CodeList" onchange="CodeChange();">
    $options
    </select> 
    <h3>Saved Codes</h3>
    <form method="post" action="/evo/avsaveprocess.php"><div>
    <input type="hidden" name="Action" value="SAVE" />
    <input type="hidden" name="CodeId" id="CodeId" value="0" />
    <label>Description:</label>
    <input type="text" name="CodeDescription" size="40" maxlength="50" id="CodeName" value="" /><br/>
    <textarea rows="10" name="Code" id="CodeValue" onload="resize()">
    </textarea><br/>
    <input class="btn" type="submit" value="Save" />
    </div></form>
    <script type="text/javascript">
    //<![CDATA[
    $js
    sel = document.getElementById('CodeList');
    txt = document.getElementById('CodeValue');
    var maxHeight = (window.innerHeight - txt.offsetTop) - 40;
    function CodeChange(){
    txt.style.height =  '100px';
    var ndx = sel.selectedIndex; 
    var temp = contents[ndx].replace(/&lt;/g,'<');
    temp = temp.replace(/&gt;/g,'>');
    txt.value= temp.replace(/<br>/g,"\\n");
    var h = txt.scrollHeight;
    if(h > maxHeight){h = maxHeight;}
    txt.style.height = h + 'px';
    }
    //]]>
    </script></body></html>
    EOT;
    ob_end_flush();
    

    【讨论】:

    • 谢谢你误解了我忘记了 .hide css 并且可以努力找出那部分。我遇到的问题更多的是在 jquery 中尝试在选择我设置为选项值 = 0 的下拉选择(添加新代码)时使文本区域为空
    • 您想更改&lt;select&gt;&lt;option&gt;THIS TEXT&lt;/option&gt;&lt;/select&gt; 中的文字,对吗?
    • 是的,正确,当他们选择从 php 中正确显示的其他下拉项目时,当他们选择 我想要文本框为空白。
    • 感谢您的解释。基本上,我只是想选择一个字段为空白的选项,以便他们可以使用表单来保存数据,然后在他们从下拉按钮中选择数据时查看数据。我想一个简单的 jquery 按钮来清除表单就可以了,然后我就不用担心那个按钮了。
    • 误解了,我输入了你所拥有的编码,按钮显示,当然当我放入 .hide{display:none};但是当我放入 JS 时,它说无法读取 null 的属性,当然在下拉选择中没有任何反应使删除按钮出现。我输入的 Javascript 是
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多