Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.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
Php 使用jquery从插入到src标记的html输入字段中获取值_Php_Javascript_Jquery_Html - Fatal编程技术网

Php 使用jquery从插入到src标记的html输入字段中获取值

Php 使用jquery从插入到src标记的html输入字段中获取值,php,javascript,jquery,html,Php,Javascript,Jquery,Html,我有一个输入字段,我正在通过浏览按钮插入值,现在我想将该值从输入字段复制到一个src标记,以便我可以预览用户刚刚上传的图像 这是我的密码 <input class="text-input" type="text" name="logo" id="logo" onclick="tinyBrowserPopUp('image','logo','client_logos');"/> 我想将所选值从上述输入字段复制到 <img src="myimage" /> 您不能直

我有一个输入字段,我正在通过浏览按钮插入值,现在我想将该值从输入字段复制到一个src标记,以便我可以预览用户刚刚上传的图像

这是我的密码

<input class="text-input" type="text" name="logo" id="logo" onclick="tinyBrowserPopUp('image','logo','client_logos');"/>

我想将所选值从上述输入字段复制到

<img src="myimage" /> 

您不能直接从用户计算机上显示它,而是需要先将其上传到服务器,然后再显示。使用ajax上传文件将产生与您想要的效果相同的效果。另请看:

更新:由于服务器上已有图像,请尝试下面的代码

请尝试以下代码:

HTML:

<input class="text-input" type="text" name="logo" id="logo" onclick="tinyBrowserPopUp('image','logo','client_logos');" />
<img src="myimage" />
setInterval(react, 5000);
function react() {
    document.getElementByTagName("img").src = document.getElementByName("logo").value;
}
<input class="text-input" class="classhere" type="text" name="logo" id="logo" />
<div class="imagearea"></div>
$("body").on("change",".classhere",function(){
  //Equivalent of getElementById
  var fileInput = $(this)[0];//returns a HTML DOM object by putting the [0] since it's really an associative array.
  var file = fileInput.files[0]; //there is only '1' file since they are not multiple type.

  var reader = new FileReader();
  reader.onload = function(e) {
       // Create a new image.
       var img = new Image();

       img.src = reader.result;
       $(".imagearea").html(img);
   }

   reader.readAsDataURL(file);//attempts to read the file in question.
});

您不能直接从用户的计算机上显示它,而是需要先将其上载到服务器,然后再显示。使用ajax上传文件将产生与您想要的效果相同的效果。另请看:

更新:由于服务器上已有图像,请尝试下面的代码

请尝试以下代码:

HTML:

<input class="text-input" type="text" name="logo" id="logo" onclick="tinyBrowserPopUp('image','logo','client_logos');" />
<img src="myimage" />
setInterval(react, 5000);
function react() {
    document.getElementByTagName("img").src = document.getElementByName("logo").value;
}
<input class="text-input" class="classhere" type="text" name="logo" id="logo" />
<div class="imagearea"></div>
$("body").on("change",".classhere",function(){
  //Equivalent of getElementById
  var fileInput = $(this)[0];//returns a HTML DOM object by putting the [0] since it's really an associative array.
  var file = fileInput.files[0]; //there is only '1' file since they are not multiple type.

  var reader = new FileReader();
  reader.onload = function(e) {
       // Create a new image.
       var img = new Image();

       img.src = reader.result;
       $(".imagearea").html(img);
   }

   reader.readAsDataURL(file);//attempts to read the file in question.
});

将id添加到img标签并使用

var imagepath = $('#logo').val();
$('#myimg').attr('src', imagepath);

在更改输入时启动的函数内部

将id添加到img标签并使用

var imagepath = $('#logo').val();
$('#myimg').attr('src', imagepath);

在更改输入时启动的函数内部

我使用下面的代码实现了这一点。我喜欢将函数放在body上,这样即使之后通过AJAX添加了类,“change”命令仍然会触发事件

我的方法确实使用jQuery

HTML:

<input class="text-input" type="text" name="logo" id="logo" onclick="tinyBrowserPopUp('image','logo','client_logos');" />
<img src="myimage" />
setInterval(react, 5000);
function react() {
    document.getElementByTagName("img").src = document.getElementByName("logo").value;
}
<input class="text-input" class="classhere" type="text" name="logo" id="logo" />
<div class="imagearea"></div>
$("body").on("change",".classhere",function(){
  //Equivalent of getElementById
  var fileInput = $(this)[0];//returns a HTML DOM object by putting the [0] since it's really an associative array.
  var file = fileInput.files[0]; //there is only '1' file since they are not multiple type.

  var reader = new FileReader();
  reader.onload = function(e) {
       // Create a new image.
       var img = new Image();

       img.src = reader.result;
       $(".imagearea").html(img);
   }

   reader.readAsDataURL(file);//attempts to read the file in question.
});
这种方法使用HTML5文件系统API读取图像并将其放入新的javascript img对象中。这里的关键是readAsDataURL。如果使用chrome inspector,您会注意到图像是以base64编码存储的


读卡器是异步的,这就是它使用onload回调函数的原因。因此,请确保任何需要图像的重要代码都在onLoad中,否则您可能会得到意外的结果

我用下面的代码得到了这个。我喜欢将函数放在body上,这样即使之后通过AJAX添加了类,“change”命令仍然会触发事件

我的方法确实使用jQuery

HTML:

<input class="text-input" type="text" name="logo" id="logo" onclick="tinyBrowserPopUp('image','logo','client_logos');" />
<img src="myimage" />
setInterval(react, 5000);
function react() {
    document.getElementByTagName("img").src = document.getElementByName("logo").value;
}
<input class="text-input" class="classhere" type="text" name="logo" id="logo" />
<div class="imagearea"></div>
$("body").on("change",".classhere",function(){
  //Equivalent of getElementById
  var fileInput = $(this)[0];//returns a HTML DOM object by putting the [0] since it's really an associative array.
  var file = fileInput.files[0]; //there is only '1' file since they are not multiple type.

  var reader = new FileReader();
  reader.onload = function(e) {
       // Create a new image.
       var img = new Image();

       img.src = reader.result;
       $(".imagearea").html(img);
   }

   reader.readAsDataURL(file);//attempts to read the file in question.
});
这种方法使用HTML5文件系统API读取图像并将其放入新的javascript img对象中。这里的关键是readAsDataURL。如果使用chrome inspector,您会注意到图像是以base64编码存储的



读卡器是异步的,这就是它使用onload回调函数的原因。因此,请确保任何需要图像的重要代码都在onLoad中,否则您可能会得到意外的结果

您当前的javascript代码在哪里?添加代码。到目前为止你做了什么?什么不起作用?这听起来像是应该在服务器端完成的事情,因为简单地将文件名插入
字段并不能“上传”它。您不能直接从用户的文件系统访问文件。了解FileReaderAPI。看看这个例子:。读取.$(“输入[name='logo']”)。更改(函数(){});我正在使用它,但它适用于我们通过键盘键入的输入,不适用于我的情况下的选择否实际上文件axist已经在服务器上,正如您所看到的,我已经包括tinybrowser,单击函数tiny browser将文件上载到我的图像目录中,然后返回输入值name=“logo”的路径您当前的javascript代码在哪里?添加代码。到目前为止你做了什么?什么不起作用?这听起来像是应该在服务器端完成的事情,因为简单地将文件名插入
字段并不能“上传”它。您不能直接从用户的文件系统访问文件。了解FileReaderAPI。看看这个例子:。读取.$(“输入[name='logo']”)。更改(函数(){});我正在使用它,但它适用于我们通过键盘键入的输入,不适用于我的情况下的选择否实际上文件axist已经在服务器上,正如您所看到的,我已经包括tinybrowser,单击函数tiny browser将文件上载到我的图像目录中,然后返回输入值name=“logo”的路径不,实际上axist文件已经在服务器上了,正如您所看到的,我已经包括了tinybrowser,单击函数tiny browser将文件上传到我的图像目录中,然后返回输入值name=“logo”的路径。只有当我们在输入字段中键入内容时(通过键盘),onchange才起作用在这种情况下,我们不会写任何东西,我们只需从tinybrowser中选择file,然后在更改失败时,所有路径都会显示在输入字段中,直到我们单击输入框并编写一些内容OK!让我检查一下其他方法!如果你知道我的答案,我会同意的:)你能把你的小眉毛贴出来吗。。。。函数:)这会让我更容易:)是的,这里不是函数tinyBrowserPopUp(类型,formelementid,文件夹){tburl=“/admin/tinybrowser/tinybrowser.php”+“?type=“+type+”&feid=“+formelementid;if(folder!==未定义)tburl+=”&folder=“+folder+%2F”;newwindow=window.open(tburl,'tinybrowser','height=495,width=785,scrollbars=yes,Resizeable=yes');如果(window.focus){newwindow.focus()}返回false;}否实际上服务器上已经存在axist文件,正如您所看到的,我已经包含了tinybrowser,单击函数tinybrowser会将文件上传到我的图像目录中,然后返回输入值name=“logo”的路径onchange仅在我们在输入字段中键入内容(通过键盘)时才起作用在这种情况下,我们不会写任何东西,我们只需从tinybrowser中选择文件,然后在更改失败时,所有路径都会显示在输入字段中,直到我们单击输入框并写入一些内容。好的!让我检查其他方法!如果您知道一个编辑我的答案,我将批准它:)您可以发布您的小眉毛吗。。。。函数请:)这会让我更容易:)是的,这里不是函数tinyBrowserPopUp(类型,formelementid,文件夹){tburl=“/admin/TinyBrown