Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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
Jquery Ajax:div';我不能得到更新_Jquery_Ajax - Fatal编程技术网

Jquery Ajax:div';我不能得到更新

Jquery Ajax:div';我不能得到更新,jquery,ajax,Jquery,Ajax,有人能告诉我下面的代码有什么问题吗?它总是更新到新页面,而不是“内容”部分。谢谢 This is a test:<br /> <a href="/form" onclick="return updateContent()">Click me</a> <div id="content"> Update here. </div> <script> function updateContent

有人能告诉我下面的代码有什么问题吗?它总是更新到新页面,而不是“内容”部分。谢谢

This is a test:<br />
   <a href="/form" onclick="return updateContent()">Click me</a>
     <div id="content">
       Update here.
   </div>

<script>
function updateContent() {
  $.ajax({
    type: 'GET',
    url: $(this).attr('href'),
    success: function(data) {
      $('#content').html(data);
    };
 });
};
</script>
这是一个测试:
更新这里。 函数updateContent(){ $.ajax({ 键入:“GET”, url:$(this.attr('href'), 成功:功能(数据){ $('#content').html(数据); }; }); };
写以下内容,而不是写

单击我


上述方法将起作用。

因为您使用的是jQuery,所以最好正确使用它。你的链接正在做链接应该做的事情,因为你没有任何东西可以取消链接的默认行为

更改:

<a href="/form" onclick="return updateContent()">Click me</a>

梁堂。您的javascript代码中存在以下语法错误:

success: function(data) {
  $('#content').html(data);
};
不需要
}
之后。无论如何,这是正确的答案。也不要在HTML标记中使用像onclick这样的内部事件。让我们用javascript/jquery为您处理它们

This is a test:<br />
<a href="form.html" id="link">Click me</a>
<div id="content">
Update here.
</div>

<script>
$(function () {
    $('#link').bind('click', function (e) {
        e.preventDefault();

        $.ajax({
            type: 'GET',
            url: $(this).attr('href'),
            success: function(data) {
              $('#content').html(data);
            }
         });
    });
});
</script>
这是一个测试:
更新这里。 $(函数(){ $('#link').bind('click',函数(e){ e、 预防默认值(); $.ajax({ 键入:“GET”, url:$(this.attr('href'), 成功:功能(数据){ $('#content').html(数据); } }); }); });
解释
1.我给你的链接指定了一个特定的ID,这样我就可以在jquery代码中访问它。

2.在链接的
单击事件中,我取消了链接行为(使用
e.preventDefault()

,解决了我的问题,非常感谢。但我还有一个问题:如果我决定使用“POST”方法怎么办?我把“GET”改为“POST”,脚本不起作用。
success: function(data) {
  $('#content').html(data);
};
This is a test:<br />
<a href="form.html" id="link">Click me</a>
<div id="content">
Update here.
</div>

<script>
$(function () {
    $('#link').bind('click', function (e) {
        e.preventDefault();

        $.ajax({
            type: 'GET',
            url: $(this).attr('href'),
            success: function(data) {
              $('#content').html(data);
            }
         });
    });
});
</script>