将多行字符串值提供给jquery方法

How do I feed string variable holding multiline value in it to jquery method like prepend? I need it to construct a form pass by drupal search module like - $('#divsearch').prepend('<?php print $search_box; ?>'). Thanks a bunch.

dongping6974
dongping6974 -输出中的换行符会引发语法错误。
接近 10 年之前 回复
dongzhi1822
dongzhi1822 我是唯一一个不了解最终目标的人吗?将字符串传递给前置有什么问题?
接近 10 年之前 回复

2个回答

Use json_encode() to encode the string, like this:

$('#divsearch').prepend('<?php print json_encode($search_box); ?>');

This will encode newlines as instead of literally rendering them...giving you your current syntax errors.

douqie1852
douqie1852 +1比我好,难怪你在每周排名中排名第一:)
接近 10 年之前 回复
duanhui1185
duanhui1185 嘿嘿恭喜100k
接近 10 年之前 回复

I think the problem is that you can have carriage returns or new lines in you string lets take out the php and show what i mean

$('#divsearch').prepend('<div />');

this would work

$('#divsearch').prepend('<div>some text that goes 
      in a div</div>'); 

this does not work

$('#divsearch').prepend('<div>some text that goes'+
      'in a div</div>'); 

this woould

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问