dongxia4880 2012-11-07 05:40
浏览 90
已采纳

如何从逗号分隔的字符串变量中删除一个值?

I want to remove one value from comma separated string variable Eg hdnListCL ="'ABC','PQR','XYZ'" i want to remove any one of them, how i can do this without converting this into array? html code

<input type="hidden" name="hdnlistCL" id="hdnlistCL"/>
<table>
 <tr>
    <td>
            <div id="ProjList"><?php print $strLocName; ?></div><br/>
         <input type="text" name="txtCommonLocation" id="txtCommonLocation" size="40" value=""/>
         <img src="images/plus.gif" title="Click Here" onclick="AddNewLocation()" style='cursor:pointer;' align="right"/>  </td>    
     </tr>
</table>

javascript

<script type="text/javascript" src="../scripts/jquery.min.js"></script>
<script type="text/javascript">
function AddNewLocation()
{
    var listCL = "";
    this.strProjName;
    if ( typeof strProjName == 'undefined' ) {
          strProjName = '';
    }
    var newLoc = document.getElementById('txtCommonLocation').value;
    document.getElementById('txtCommonLocation').value = "";
    if(document.getElementById('hdnlistCL').value == '')
    { 
       document.getElementById('hdnlistCL').value =newLoc;
    }
    else
    {
      document.getElementById('hdnlistCL').value += ","+newLoc;
    }
      listCL  = newLoc; 
      if(listCL != '')  
        {
             strProjName = strProjName + '<div id="'+listCL+'">'+listCL+'<div  class="close" onclick="removeLocation(\''+listCL+'\')"></div></div>';            
        }
    //  alert(strProjName);
        $('#ProjList').html(strProjName);   
}
function removeLocation(pLocation)
{
    var hdnListLocation = document.getElementById('hdnlistCL').value;
    if(window.confirm("Are you sure you want to delete this?"))
                {      
                            url = 'test_st.php';
                            $.post(
                                url,
                                {"act":"Delete","DelLocation":pLocation,"hdnListLocation": hdnListLocation},
                                function(responseText){
                                    alert(responseText);
                                    return false;
                                  window.location="test_st.php";                    
                                },
                                "html"
                            );
                            return false;
                }   
}
</script>

php code

<?php
 if(isset($_POST['act']))   
       $act = $_POST['act'];

        if($act == "Delete")
        {
            $arrhdnListLocation     = explode(",", $_POST['hdnListLocation']);
            if(in_array($_POST['DelLocation'],$arrhdnListLocation))
            {
            print("I want to remove'".$_POST['DelLocation']."' from hdnlistCL");        
                    exit();
            }
            else
            {
                print("No");
                exit();
            }

        }
?>

enter image description here

when i click close button i want to remove that location from hdnlistCL. how i can do this? value of hdnListCL is ABC,PQR,XYZ i want to remove PQR from this

  • 写回答

2条回答 默认 最新

  • dqaq59269 2012-11-07 05:45
    关注

    Please Try this

    $hdnListCL ="'ABC','PQR','XYZ'";
    $hdnListCL=explode(',',$hdnListCL);
    $index = array_search('PQR',$hdnListCL);
    if($index !== false){
        unset($hdnListCL[$index]);
    }
    $hdnListCL=implode(',',$hdnListCL);
    print_r($hdnListCL);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?