简体   繁体   English

为什么我无法使用javascript从输入中获取价值?

[英]Why i can not get value from input using javascript?

When i press on click here It's wil be alert blank and blank value. 当我按click here ,将警告空白和空白值。

I want to know why not alert 400 and 400 我想知道为什么不提醒400400

test1.php test1.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<form id="first_image_data_url_fid" style="display: block;">
    <input type="text" name="first_image_data_url" id="first_image_data_url"/>

    <input type="text" name="first_image_data_dimension_width" id="first_image_data_dimension_width"/>
    <input type="text" name="first_image_data_dimension_height" id="first_image_data_dimension_height"/>
</form>
<span id="mySpan_check_image_data_width_height"></span>

<div onclick="test_fn()">click here</div>

<script language="JavaScript" type="text/javascript">
function test_fn()
{
var first_image_data_url = "https://pbs.twimg.com/profile_images/839721704163155970/LI_TRk1z_400x400.jpg";
document.getElementById("first_image_data_url").value = first_image_data_url;
document.getElementById("first_image_data_dimension_width").value = "";
document.getElementById("first_image_data_dimension_height").value = "";
$.ajax
(
    {
        url: 'test2.php',
        type: 'POST',
        data: $('#first_image_data_url_fid').serialize(),
        cache: false,
        success: function (data) {
            $('#mySpan_check_image_data_width_height').html(data);
            //get_first_image_data_width_height();
        }
    }
)

var item = get_first_image_data_width_height();
}
</script>


<script>
function get_first_image_data_width_height(){
    var item;
    var first_image_data_dimension_width_val = document.getElementById("first_image_data_dimension_width").value;
    alert(first_image_data_dimension_width_val);

    var first_image_data_dimension_height_val = document.getElementById("first_image_data_dimension_height").value;
    alert(first_image_data_dimension_height_val);

var item = "0";

    return item;
}
</script>

test2.php test2.php

<?PHP
session_start();
include("connect.php");
$first_image_data_url = mysqli_real_escape_string($db_mysqli,$_POST['first_image_data_url']);

$first_image_data_dimension = getimagesize($first_image_data_url);
$first_image_data_dimension_width = $first_image_data_dimension[0];
$first_image_data_dimension_height = $first_image_data_dimension[1];
?>

<script>
document.getElementById("first_image_data_dimension_width").value = "<?PHP echo $first_image_data_dimension_width; ?>";
document.getElementById("first_image_data_dimension_height").value = "<?PHP echo $first_image_data_dimension_height; ?>";
</script>

If I understood your question correctly, you want to know why alerts gives you an empty string even if you see 400 and 400 in your inputs. 如果我正确理解了您的问题,那么您想知道即使在输入中看到400和400,为什么警报也会为您提供一个空字符串。 It is because JS is asynchronous language and your timeline looks like this: 这是因为JS是异步语言,并且您的时间轴如下所示:

function test_fn()
{
var first_image_data_url = "...";
// (1) setting an empty values
document.getElementById("first_image_data_url").value = first_image_data_url;
document.getElementById("first_image_data_dimension_width").value = "";
document.getElementById("first_image_data_dimension_height").value = "";
// (2) starting AJAX call
$.ajax
(
    {
        url: 'test2.php',
        type: 'POST',
        data: $('#first_image_data_url_fid').serialize(),
        cache: false,
        success: function (data) {
            // (4) END of AJAX call

            $('#mySpan_check_image_data_width_height').html(data);
            //get_first_image_data_width_height();
            // you should get your 400 and 400 here
            //var item = get_first_image_data_width_height();

        }
    }
)
// (3) showing the result
var item = get_first_image_data_width_height();
}

If you look carefully, you will see that you try to alert values before they come. 如果仔细看,您会发现您尝试在值出现之前发出alert

BTW, you don't need to do mysqli_real_escape_string before get_image_size . 顺便说一句,你不需要做mysqli_real_escape_string之前get_image_size

If you don't do any calculations on the image nor downloading it, you can obtain it dimensions by pure JS. 如果您不对图像进行任何计算或下载,都可以通过纯JS获得尺寸。 See this answer 看到这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM