从iframe为多个iframe添加隐藏输入字段的值

时间:2016-11-07 04:29:20

标签: javascript php jquery html iframe

我目前正在iframe中上传文件,并尝试将要传递到父页面上iframe下方隐藏输入字段的文件名称。

我目前的upload.php如下所示:

<script type="text/javascript">
$(document).ready(function($){
if($("#fileName").val() != ""){
    var name = $("#fileName").val();
    $(this, window.parent.document).next('input').val(name);
}});
</script>

<body>
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 10000000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" && $imageFileType != "xls" && $imageFileType != "xlsx"
&& $imageFileType != "doc" && $imageFileType != "docx" && $imageFileType != "pdf") {
    echo "Sorry, only JPG, JPEG, PNG, GIF, XLS, XLSX, DOC, DOCX & PDF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
        echo '<input type="hidden" value="'. basename( $_FILES["fileToUpload"]["name"]). '" id="fileName">';
} else {
    echo "Sorry, there was an error uploading your file.";
}
}
?>
</body>

我的主要问题是尝试选择其所在的特定iframe,因为它在同一页面上多次使用。

<script type="text/javascript">
$(document).ready(function($){
if($("#fileName").val() != ""){
    var name = $("#fileName").val();
    **$(this, window.parent.document).next('input').val(name);**
}});
</script>

0 个答案:

没有答案