Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将变量与'';在jQuery中_Javascript_Jquery - Fatal编程技术网

Javascript 如何将变量与'';在jQuery中

Javascript 如何将变量与'';在jQuery中,javascript,jquery,Javascript,Jquery,您好,我正在尝试使用jQuery使用函数onchange with Ajax创建一个输入。这是输入: <input type="file" class="hidden-print btn-sm" id="btn1" onchange="document.getElementById('blah').src = window.URL.createObjectURL(this.files[0])"> 但我得到了这个控制台错误: 无法将属性“src”

您好,我正在尝试使用jQuery使用函数onchange with Ajax创建一个输入。这是输入:

<input type="file" class="hidden-print btn-sm" id="btn1"
                   onchange="document.getElementById('blah').src = window.URL.createObjectURL(this.files[0])">
但我得到了这个控制台错误:

无法将属性“src”设置为null。我知道这是因为('blah')问题,因为HTML代码工作正常


当变量值附加到字符串中时,需要在变量值周围加引号。为此,您需要对字符串中的引号进行转义,以便它们不会被解释为分隔字符串,为此,您可以使用
\'
。试试这个:

getElementById(' + "blah" + ') rest of the code...
html+='';

就这样。谢谢兄弟!!帮个忙,使用DOM(或jQuery的等效)方法。我不知道DOM,但谢谢!!!我还有一件事要学。
var blah  = 'blah';
html += '<input type="file" class="hidden-print btn-sm" id="btn1" onchange="document.getElementById('+ blah +').src = window.URL.createObjectURL(this.files[0])">';
getElementById(' + "blah" + ') rest of the code...
html += '<input type="file" class="hidden-print btn-sm" id="btn1" onchange="document.getElementById(\''+ blah + '\').src = window.URL.createObjectURL(this.files[0])">';