Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.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
$.ajax不从php执行jquery代码_Php_Jquery_Html - Fatal编程技术网

$.ajax不从php执行jquery代码

$.ajax不从php执行jquery代码,php,jquery,html,Php,Jquery,Html,我试图从ajax调用的php页面执行jquery代码 更改div.error的html 下面是代码(如果有更简单的方法,只需告诉/张贴):-no$.load HTML/jQuery:jQuery.js已链接 <input type='text' id='test'> <div id='error' style='display:inline-block;'>CHANGE HERE</div><bR> <input type='submit'

我试图从ajax调用的php页面执行jquery代码

更改div.error的html

下面是代码(如果有更简单的方法,只需告诉/张贴):-no$.load

HTML/jQuery:jQuery.js已链接

<input type='text' id='test'>
<div id='error' style='display:inline-block;'>CHANGE HERE</div><bR>
<input type='submit' value='run' id='button'>

<script type='text/javascript'>
$('#button').click(function(){
$.ajax({
    url: "test.php",
    type: "POST",
    data: "test="+ test,
    success: function(jqXHR){
    },
    error: function() {
        alert('error');
    }
});
});
</script>

在这里更改
$(“#按钮”)。单击(函数(){ $.ajax({ url:“test.php”, 类型:“POST”, 数据:“测试=”+测试, 成功:函数(jqXHR){ }, 错误:函数(){ 警报(“错误”); } }); });
test.php

<script type='text/javascript'>
$('#error').html('working');
</script>

$('#error').html('working');

您正在使用的
test
变量似乎没有在任何地方声明。首先尝试硬编码。另外,首选
{key:value}
语法,因为它确保在发送到服务器时正确地对值进行url编码:

data: { test: 'some value' }
如果要使用变量,请确保已声明该变量:

$('#button').click(function() {
    var test = 'foo bar';
    $.ajax({
        url: 'test.php',
        type: 'POST',
        data: { test: test },
        success: function(data) {

        },
        error: function() {
            alert('error');
        }
    });
});
另外,如果希望在php脚本中执行javascript,请删除
标记:

$('#error').html('working');
并在
$调用中将
数据类型
参数指定为
脚本
。ajax
调用:

dataType: 'script'
另一种可能是让
test.php
脚本简单地返回值:

working
然后在成功回调中刷新DOM:

success: function(data) {
    $('#error').html(data);
},

您的test.php脚本应该输出您想要显示的内容

它应该看起来像:

echo "<html>";
echo "<div>working</div>";
echo "</html>";
echo”“;
呼应“工作”;
回声“;

所以它会返回一些东西。现在,您只返回javascript。

来自
test.php
内部的javascript不会运行,因为您从未告诉它运行。它不会因为你装了就跑。它需要在DOM中才能运行

试试这个:

$.ajax({
    url: "test.php",
    type: "POST",
    data: "test="+ test,
    success: function(data){
        $(data).filter('script').appendTo('head');
    },
    error: function() {
        alert('error');
    }
});

您到底想在这里做什么?ajax的目的是使用javascript(客户端处理)向服务器发送请求(php服务器端处理),而不引起刷新。在您的情况下,您要求服务器运行客户端脚本,这将不起作用。你的
test.php
文件应该有生成输出的php代码。是的,我忘了添加它,但我只是想让div变成changeSure,但要确保你为此编写了有效的javascript。如果不声明正在使用的变量,将出现javascript错误,脚本将无法运行。
test
变量可能存在。他有一个id为
test
的元素。一些浏览器会将其变成一个变量。这意味着他可能正在发布
test=[object-HTMLInputElement]
:-POK,请参阅我的更新答案。为$.ajax调用指定
dataType:'script'
,并从
test.php
中删除
标记,只保留jquery代码。