Ajax,使用 div 标签设置 javascript 值

发布于 2025-01-06 11:54:06 字数 1149 浏览 0 评论 0原文

我可以使用 ajax 轻松设置 div 标签。不过我想使用 ajax 设置 javascript 值。我曾尝试使用 javaScriptVariable = div.innerHTML 读取 div 标签,但它显示为空白。

有人可以帮忙吗?提前致谢。

<html>
<head>
<title>Log In</title>

<!-- script tag for ajax jquery -->
<link href='style.css' type="text/css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js" type="text/javascript"></script>

<script type="text/javascript">

$(document).ready(function()

              {
              //Get Data ajax function 
              $.post(
                     'getData.php',
                     {

                     },                                           
                     function(response)
                     {
                     $('#name').html($('#1' , response).html());
                     $('#sname').html($('#2' , response).html());                                           
                     })
              return false;
              })

var x;
x = name.innerHTML;
document.write(x);

</script>

</head>

<body>

I can easily set div tags using ajax. However i want to set javascript values using ajax. I had tried reading the div tags using javaScriptVariable = div.innerHTML but it comes out blank.

Can anyone please help? thanks in advance.

<html>
<head>
<title>Log In</title>

<!-- script tag for ajax jquery -->
<link href='style.css' type="text/css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js" type="text/javascript"></script>

<script type="text/javascript">

$(document).ready(function()

              {
              //Get Data ajax function 
              $.post(
                     'getData.php',
                     {

                     },                                           
                     function(response)
                     {
                     $('#name').html($('#1' , response).html());
                     $('#sname').html($('#2' , response).html());                                           
                     })
              return false;
              })

var x;
x = name.innerHTML;
document.write(x);

</script>

</head>

<body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

日久见人心 2025-01-13 11:54:06

如果你不向 jquery 的 html() 方法传递值,它将返回当前值:

var x = $('#name').html();
document.write(x);

if you don't pass a value to jquery's html() method, it will return the current value:

var x = $('#name').html();
document.write(x);
兔小萌 2025-01-13 11:54:06

这是因为这两行在脚本加载后立即执行。我假设该元素此时可能为空。

x = name.innerHTML;
document.write(x);

当 ajax 调用返回时,您需要设置 x

$(document).ready(function()
{
    //Get Data ajax function 
    $.post(
        'getData.php',
        {

        },                                           
        function(response)
        {
            $('#name').html($('#1' , response).html());
            $('#sname').html($('#2' , response).html());

            var x = $('#name').html();
            document.write(x);
        })
    return false;
});

That is because these two lines execute as soon as the script loads. I am assuming that that element is probably empty at this time.

x = name.innerHTML;
document.write(x);

You will need to set x when the ajax call comes back.

$(document).ready(function()
{
    //Get Data ajax function 
    $.post(
        'getData.php',
        {

        },                                           
        function(response)
        {
            $('#name').html($('#1' , response).html());
            $('#sname').html($('#2' , response).html());

            var x = $('#name').html();
            document.write(x);
        })
    return false;
});
我一向站在原地 2025-01-13 11:54:06

您遇到问题是因为您没有在任何地方定义变量name

为什么还要费劲去遍历 div 呢?直接转到var:

var x;
// ...

                 function(response)
                 {
                 $('#name').html($('#1' , response).html());
                 $('#sname').html($('#2' , response).html());
                 //this will set x, you can do this instead of, or in addition to the above
                 x = $('#1' , response).html();
                 })

You are having a problem because you do not have a variable name defined anywhere.

Why bother going through the div anyway? just go to the var directly:

var x;
// ...

                 function(response)
                 {
                 $('#name').html($('#1' , response).html());
                 $('#sname').html($('#2' , response).html());
                 //this will set x, you can do this instead of, or in addition to the above
                 x = $('#1' , response).html();
                 })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文