Ajax,使用 div 标签设置 javascript 值
我可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你不向 jquery 的 html() 方法传递值,它将返回当前值:
if you don't pass a value to jquery's html() method, it will return the current value:
这是因为这两行在脚本加载后立即执行。我假设该元素此时可能为空。
当 ajax 调用返回时,您需要设置
x
。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.
You will need to set
x
when the ajax call comes back.您遇到问题是因为您没有在任何地方定义变量
name
。为什么还要费劲去遍历 div 呢?直接转到var:
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: