如何调用JavaScript在Ajax响应? IE浏览器:关闭成功后的一种形式DIV浏览器、形式、Ajax、JavaScript

2023-09-10 14:29:11 作者:蝴蝶、停在谁的锁骨上妖娆

我有,当你提交,它发送的数据进行验证,以另一种PHP脚本通过AJAX的形式。验证错误echo'd回来在我的形式股利。成功消息还,如果验证通过返回。

I have a form that when you submit it, it sends the data for validation to another php script via ajax. Validation errors are echo'd back in a div in my form. A success message also is returned if validation passes.

的问题是,表单提交和验证成功仍显示之后。我想躲在成功后的股利。

The problem is that the form is still displayed after submit and successful validation. I want to hid the div after success.

所以,我写了这个简单的CSS方法的形式显示在页面调用时,工作正常。

So, I wrote this simple CSS method which works fine when called from the page the form is displayed on.

现在的问题是,我似乎无法通过调用返回code中的隐藏脚本。我可以返回HTML一样

The problem is that I cannot seem to call the hide script via returned code. I can return html like

echo "<p>Thanks, your form passed validation and is being sent</p>";

所以,我以为我可以简单地附和之后的另一行

So I assumed I could simply echo another line after that

echo "window.onload=displayDiv()";

里面的脚本标记(我不能在这里显示)...

inside script tags (which I cannot get to display here)...

和它会隐藏窗体DIV。

and that it would hide the form div.

这是行不通的。我假设的问题是,JavaScript是返回错误,而不是由浏览器PTED间$ P $ ...

It does not work. I am assuming that the problem is that the javascript is being returned incorrectly and not being interpreted by the browser...

我如何通过从我的验证脚本返回的数据调用网页上我的隐藏的脚本?我可以回显文本,但该脚本调用是无效的。

How can I invoke my 'hide' script on the page via returned data from my validation script? I can echo back text but the script call is ineffective.

谢谢!

这是在页面的形式...

This is the script on the page with the form...

我可以调用它来显示/隐藏的东西,如的onclick =displayDiv(),而在表单上,​​但我不希望用户调用这个...它被称为一个成功的验证结果当我写的结果返回给DIV ...

I can call it to show/hide with something like onclick="displayDiv()" while on the form but I don't want the user to invoke this... it has be called as the result of a successful validation when I write the results back to the div...

<script language="javascript" type="text/javascript">
    function displayDiv()
    {
        var divstyle = new String();
        divstyle = document.getElementById("myForm").style.display;
        if(divstyle.toLowerCase()=="block" || divstyle == "")
        {
            document.getElementById("myForm").style.display = "none";
        }
        else
        {
            document.getElementById("myForm").style.display = "block";
        }
    }
    </script>

PS:我使用的是mootools.js库表单验证,如果这个重要的语法。

PS: I am using the mootools.js library for the form validation if this matters for the syntax..

Ajax调用是:


window.addEvent('domready', function(){
$('myForm').addEvent('submit', function(e) {
new Event(e).stop();
var log = $('log_res').empty().addClass('ajax-loading');
this.send({
update: log,
onComplete: function() {
log.removeClass('ajax-loading');
}
});
});
});

股利ID日志是Ajax调用回文本(验证错误和成功消息)和装载图形显示

Div ID log is where the ajax call back text (validation errors and success message) and loading graphic appear

推荐答案

这是http://stackoverflow.com/questions/2950545/how-to-make-js-execute-in-html-response-received-using-ajax/2950719#2950719在这里我提供了选择的解决方案。

This is a duplicate of http://stackoverflow.com/questions/2950545/how-to-make-js-execute-in-html-response-received-using-ajax/2950719#2950719 where I provided the chosen solution.

var response = "html\<script type=\"text/javascript\">alert(\"foo\");<\/script>html";
 var reScript = /\<script.*?>(.*)<\/script>/mg;
 response = response.replace(reScript, function(m,m1) {
     eval(m1); //will run alert("foo");
     return "";
 });
alert(response); // will alert "htmlhtml"