简体   繁体   English

使用AJAX / Jquery替换div

[英]Using AJAX/Jquery to Replace div

I am trying to set the content of an empty div after an AJAX call with the data message. 我试图用data消息在AJAX调用后设置一个空div的内容。 I also wired the function to my CHtml::submitButton , which should call the function when clicked, but nothing is happening. 我还将功能连接到CHtml::submitButton ,单击该功能时应调用该功​​能,但没有任何反应。 Any suggestions? 有什么建议么?

<div id="myResult">        
</div>

JavaScript : JavaScript

function successMessage(){
    $.ajax({
        type: "GET",
        data: "<div> Replace div with this contentf</div>",
        success: function(data){
            $('myResult').html(data);
        }
    })
}

PHP: PHP:

echo CHtml::beginForm(array('myForm'), 'get', array('form'));
echo '<div class="row form">';
echo '<div class="row buttons">';
echo CHtml::submitButton('Download Content', array('htmlOptions' => 'successMessage()'));
echo '</div>';
echo '</div>';

Try this: 尝试这个:

 $.ajax({
             url: "test.html",
            type: "GET",
            data: "<div> Replace div with this contentf</div>",
            success: function(data){
                $('#myResult').html(data);
            }
        })

selector jQuery incorrect in response ajax. 选择器jQuery在响应Ajax中不正确。 and define Url in ajax. 并在ajax中定义Url。

Your problem relies in the following line: 您的问题取决于以下行:

$('myResult').html(data);

Here, you are trying to do a jquery selection to an element, which you are not using in your html (this is only possible via pollyfils). 在这里,您尝试对不在HTML中使用的元素进行jquery选择(这只能通过pollyfils实现)。 So you have to select the element by its ID: 因此,您必须通过其ID选择元素:

$('#myResult').html(data);

And another thing i've seen, what is the url where you are doing the request? 我看到的另一件事是,您在执行请求的网址是什么?

<script>
    function successMessage(){
        $.ajax({
            type: "GET",
            url: "/please/add/an/url/",
            data: "<div> Replace div with this contentf</div>",
            success: function(data){
                $('myResult').html(data);
            }
        })
    }
</script>

first of all, when you are using onclick function like this : 首先,当您使用像这样的onclick函数时:

<input type="submit" onclick="successMessage()">

you should use this instead: 您应该改用以下代码:

<input type="submit" onclick="successMessage();result false;">

but when you are already using jquery, then better approach is: 但是当您已经在使用jQuery时,更好的方法是:

 $( document ).ready(function() {
        successMessage(){
           // your ajax goes here
        }

        $('#myResult').click(function(e){
          e.preventDefault();
          successMessage();
        });
     });

Then you need to repair your successMessage function. 然后,您需要修复您的successMessage函数。 You see, the data you are setting there are not the data, that are coming out as an output. 您会看到,正在设置的数据没有作为输出输出的数据。 If you need ajax then you probably want to get the result from some php script on some other url. 如果您需要ajax,则可能要从其他网址上的某些php脚本获取结果。 Then you should do it like this : 然后,您应该这样做:

function successMessage(){
        $.ajax({
            type: "GET",
            url : 'index2.php',
            dataType: "json",
            data: { mydata: '<div> Replace div with this content</div>'},
            success: function(data){             
                $('#myResult').html(data);
            }
        })
    }

Then you need a php file named index2.php which can look like this : 然后,您需要一个名为index2.php的php文件,该文件如下所示:

<?php
    echo json_encode($_GET['variable']);
?>

And i dont know if this your line : 而且我不知道这是否您的台词:

echo CHtml::submitButton('Download Content', array('htmlOptions' => 'successMessage()'));

also put the </form> tag after the form to close it. 还将</form>标记放在</form>后面以将其关闭。

This should work for you. 这应该为您工作。 I tried it and it works fine. 我试过了,效果很好。

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

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