dqjcb132285 2013-05-29 15:57
浏览 52
已采纳

我正在尝试将此php函数转换为javascript函数

I have a PHP function to create an address from the data retrieved from the database. I want to use the same feature to make a JavaScript function that will do the same. What it does is take the field and if it is empty it does nothing but if there is data then it will append ", " a comma and space.

$parts = array(
          $club['clubAdd1'],
          $club['clubAdd2'],
          $club['clubCity'],
          $club['clubCounty'],
          $club['clubPostcode'],
        );

$address = array();

foreach ($parts as $part){
    if ('' != $part){
        $address[] = $part;
    }
}

$address = implode(', ', $address);

My current attempt is the function is called everytime the keyup is preformed on the form fields (test purposes only)

function autoAddress(){
var address = "";
var address1 = document.address.address1.value;
var address2 = document.address.address2.value;
var city = document.address.city.value;
var county = document.address.county.value;
var postcode = document.address.postcode.value;

var parts = new array[
        address1,
        address2,
        city,
        county,
        postcode
      ];
var testAddress = new array();
foreach(parts as part){
  if ('' != part){
    testAddress[] = part;
  }
}
testAddress = array.join(', ', testAddress);
alert(testAddress);

}

  • 写回答

1条回答 默认 最新

  • douchui1657 2013-05-29 16:06
    关注
    var parts = [
              club['clubAdd1'],
              club['clubAdd2'],
              club['clubCity'],
              club['clubCounty'],
              club['clubPostcode']
            ];
    
    var address = [];
    
    for (var i=0; i<=parts.length; i++){
        if (parts[i]){
            address.push(parts[i]);
        }
    }
    
    var joined = address.join(', ');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?