问题描述:
想要实现一种功能,就是多条手机号去重。从输入域中获取一行一行的手机号,通过ajax传参到php后台。获取去重后的数据赋值到另一个输入域中。
解决办法:
html页面代码:
<div class="col-lg-6">
<div class="panel panel-info">
<div class="panel-heading">
<button class="btn btn-info">用英文逗号分割手机号</button>
<button class="btn btn-primary pull-right" type="submit">
点击过滤
</button>
</div>
<div class="panel-body">
<textarea name="str" class="form-control" rows="20"></textarea>
</div>
</div>
</div>
<div class="col-lg-6 dfd">
<div class="panel panel-info">
<div class="panel-heading">
<button class="btn btn-info">去重结果</button>
</div>
<div class="panel-body">
<textarea class="form-control filter" rows="20"></textarea>
</div>
</div>
</div>
jquery代码:
<script type="text/javascript">
$("button[type=submit]").click(function(){
string=$("textarea[name=str]").val();
zifu=string.split("\n")+",";
/* split() 方法用于把一个字符串分割成字符串数组。*/
$.ajax({
type:'get',
url:"demo.php",
data:'str='+zifu,
success:function(data){
rex=new RegExp(',','g');
$(".filter").text(data.replace(rex,"\r\n"));
}
})
})
</script>
php代码:
$string =$_GET['str'];
$charlist = explode(',', $string);
$c= array_unique( $charlist );
$b=implode(',', $c);
echo $b;