dongzai3917 2014-12-21 11:38
浏览 55
已采纳

Javascript:缺少; 在语句之前 - 转义错误

On the host the php generated javascript drops an error of:

missing ; before statement

At the same time on localhost it works fine.

the generated code was :

try{
   obj = document.getElementById('subcat');

    }catch(e){}

try{
    obj.innerHTML = "<select name=\"sub_ad_category_id\">   
                          <option value=\"\">-</option>
                          <option value=\"5\">bootle</option>
                          <option value=\"3\">Puzzle</option>
                     </select>";
     }catch(e){}

It is generated by action.php by the next code:

if($var_type=='array') $text = '<select name="+ads_data[sub_ad_category_id]+">';
    else  $text = '<select name="sub_ad_category_id">';
    $text .=   '<option value="">-</option>';
           $res = $ads->get_ads_categories($_SESSION['lang'],$parent_id);
            while($r = mysql_fetch_array($res)){
                $text .= '<option value="'.$r['ad_category_id'].'">'.$r['category_name'].'</option>';
            }
    $text .= '</select>';

    echo "try{obj = document.getElementById('".$id."');}catch(e){}";
    echo "try{obj.innerHTML = ".$text.";}catch(e){}";
  • 写回答

1条回答 默认 最新

  • drxm72811 2014-12-21 11:41
    关注

    Look at this line ...

    obj.innerHTML = "<select name="sub_ad_category_id"><option value="">-</option></select>";
    

    ... modifications (missing some + signs and changed double quotes at value to single '')

    obj.innerHTML = "<select name=" + sub_ad_category_id + "><option value=''>-</option></select>";
    
    • The + is simple concatenation.
    • The '' single quotes is one way of including quotes in final HTML cleanly; otherwise, you could have used \"\" slash escaped quotes.

    UPDATE:

    Given what I see of your changes above, it looks like how you are building the string. Don't use the enter key when building strings. Try like this ...

    var html = "<select name=\"sub_ad_category_id\">";
    html += "<option value=\"\">-</option>";
    html += "<option value=\"5\">bootle</option>";
    html += "<option value=\"3\">Puzzle</option>";
    html += "</select>";
    ob.innerHTML = html;
    

    ... OR ...

    ob.innerHTML = "<select name=\"sub_ad_category_id\"><option value=\"\">-</option><option value=\"5\">bootle</option><option value=\"3\">Puzzle</option></select>";
    

    See this jsFiddle

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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?