错误:语法错误,无法识别的表达式:#sub_account1 选项[值=HD&NO]



我有一个php脚本,该脚本查询db并将json_encoded数据返回到javascript/jquery文件,该文件将返回的数据处理为下拉式选择框中的动态选项。

这很好。最近在我们的数据库中的一个字段中添加了一个'&'':eReqs.sub_account_code->'hd& no''正在加载页面时通过firebug查看的语法错误。当下面的查询返回此值时,jQuery/js错误会中断响应,并且在选择框中的选项中没有添加值。

错误:语法错误,未识别的表达式:#sub_account1 option [value = hd& no]

javascript文件:( json呼叫)

function get_sub_account_codes(instance, code){
  // json call to get sub-account-codes.
  $.getJSON("get-sub-account-codes.html",{code:code}, function(data){
     // empty html drop-down of options that may be showing
     $('#sub_account' + instance).html(''); // .empty()
     $('#sub_account' + instance).val('');
     // build the drop-down options
     $.each(data, function(key, value){
        $("#sub_account" + instance).append("<option value=""+value.SUB_ACCOUNT_CODE+"">"+value.SUB_ACCOUNT_CODE+"-"+value.SUB_ACCOUNT_NAME+"</option>");         
     });
  });
}

get-sub-account-codes.html php代码获取数据。从JS文件

调用
$data = array();
$dbh = SHA_DB::getConnection('webadmin');
$sql = "select distinct sub_account_code, sub_account_name
      from ereqs.kuali_account_codes 
      where account_code = upper(?) and
            sub_account_code is not null
      order by sub_account_code";
$results = $dbh->queryChecked($sql, array($code));
while($d = $results->fetchRow(DB_FETCHMODE_ASSOC))
{
    $data[] = $d;
}
# return json encoded string.
print json_encode($data);

删除&amp;'不是可行的选择。

是否可以检查&amp;在PHP文件中,并在json_encode($data)中发送回去之前以某种方式将其逃脱?还是应该在JavaScript代码中检查并更改此问题?

感谢您提供的任何提示。

为了防止您需要在引号中包装属性选择器的值所需的错误:

$('#sub_account1 option[value="HD&NO"]');

,或者您需要逃脱特殊字符,在这种情况下为&,使用\

$('#sub_account1 option[value=HD\&NO]');

相关内容

最新更新