doubiao7410 2016-10-13 01:42
浏览 68
已采纳

读取数组中的.txt文件,检查数组中是否存在变量输入

This is my HTML code:

<?php
        $filename = 'codes.txt';
        $data = file($filename);
         foreach($data as $key => $val){
             $array[] =  $val;
         }
?>
<form id="checkinput" action="#">
<input class="zipcodes" name="zipcodes" value="" type="text">
<input class="zip-sub" type="button" value="Continue" id="submit">
</form>

Thi is jQuery code:

    jQuery(document).ready(function() {
    jQuery('.zip-sub').click(function() {
        var zip = jQuery('.zipcodes').val();
        var zipcodelist = <?php echo json_encode($array); ?>;
        if( jQuery.inArray(zip, zipcodelist) != -1){
             alert('value is .txt file !');
        } else {
             alert('value is not .txt file');
        }
    });

});

This is type of some variable in .txt file:

12345
23456
56789

What i need: when i fill data in input field i can check its in variable in .txt file or not

Its working with last variable in array, not at all. I dont know where's the code is wrong ? Pls help. Thanks in advance !

  • 写回答

1条回答 默认 最新

  • douewei1665 2016-10-13 02:08
    关注

    Fix your .txt file to look more like this:

    12345,23456,56789

    Don't add in line breaks like this:

    12345, 23456, 56789

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?