jQuery将最后一个输出作为值 [英] Jquery takes last output as value

查看:100
本文介绍了jQuery将最后一个输出作为值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,它用于向数据库发送注释. 而且我有jquery和ajax代码:

I have some code and it is for sending comments to database. And I have jquery and ajax code:

<script type="text/javascript">
    $(document).ready(function() {


            $('#comm1').keypress(function(event) {

            var key = (event.keyCode ? event.keyCode : event.which);
            if (key == 13) {

                 var comment = $('#comm1').val();
                var fromstatid = '<?php echo $status->fromid; ?>';
                var status = '<?php echo $status->id;?>';
                var fromid = '<?php echo $frid; ?>';

                $.ajax({
                    method: "POST",
                    url: "d/includes/counts.php",
                    data: {u: fromid, status: status, comment: comment, fus: fromstatid},
                    success: function(status) {

                        $('#comm1').val('');

                    }

                });
                };
            }); 

    });
</script>

#comm1 is textarea for comment ..

对于php,我喜欢数据库的基本输出,问题是我的jquery仅使用数据库的最后输出作为值.换句话说,无论我在哪里输入注释,它都会发送到数据库以获取最后的输出.如果我有用于回复的按钮,然后将其发送到新页面,或者我可以将数据库的输出限制为1或仅将要评论的位置,它将可以正常工作.但是我可以在页面上所有注释都在这里做吗...另外,我试图将id设置为数据库中某个单词+ php id的输出,但是当我将其添加到jquery代码中时,它似乎可以正常工作. ... 我尝试的代码是:$("#something<?php echo $status->id; ?>").keypress..... 它会停止脚本,好像整个脚本都无法正常工作...

For php I have like basic output from database and the problem is that my jquery use only last output from database as value. In other words where ever I type a comment it send to database for the last output. It works if I have button for Reply and then it sends me to new page or something where I can limit output from database to 1 or to only that where I want to comment. But can I do it here on the page where are all comments... Also I was trying to set id as some word + php id from database for that output but it seem like when I add this to jquery code noting going to work.... The code I tried is : $("#something<?php echo $status->id; ?>").keypress..... It stops the script it looks like the whole script is not working...

我问了类似的问题,但是没有人回答我正确的问题...另外,我还在想我是否可以在此代码的jquery中使用foreach.并说出每个输出要做的事情.我真的不知道为什么它只使用页面上的最后一个输出作为值,但是如果有人有一些解释,它将对您有很大帮助?

I asked similar question but no one answered me the right question... Also I was thinking of can I like make foreach in jquery here on this code.. And say for each output do something. I really don't know why it use only last output on page as value but it will help a lot if someone have some explanation?

还是我需要为每个输出回显jquery代码?

EDITED: Or do I need to echo for each output the jquery code?

更新: 通过回显整个jquery代码进行了尝试,但无法正常工作....

UPDATE: Tried by echoing the whole jquery code and it is not working....

新: 如您所问,我有d/includes/counts.php文件和index.php

NEW: As you asked I have d/includes/counts.php file and the index.php

在counts.php内部,我只是输入通过ajax发送的数据库值... 在index.php内部,我说过我有基本的foreach数据库输出,以回显类似$ status-> text的内容,并提供注释以回显textarea,以便在其中键入文本的注释...代码太长,并且该代码没有问题,我将jQuery代码连接到所有输出(不仅是最后一个输出)时遇到问题.

Inside counts.php i just input in database values send via ajax... Inside index.php I said I have basic foreach database output to echo something like $status->text and for comment to echo textarea where I can type comment for text... Code is too long and I dont have problems with that code I have problems with connecting jquery code to all outputs not only last one....

为什么我的var status = '<?php echo $status->id; ?>';var fromstatid = '<?php echo $status->fromid; ?>';仅使用最后一个输出值?我可以在每个输出的回声中添加一些var,并在代码中在此处使用该var来导航对哪个进行评论吗?

Why does my var status = '<?php echo $status->id; ?>'; AND var fromstatid = '<?php echo $status->fromid; ?>'; use only last output values? Can I make some var inside echo for each output and use that var here in code to navigate which one is commented on?

我发现脚本也只能在一个输出上工作,例如,如果我尝试对另一输出进行注释,然后按Enter,它将无法工作....

I founded that also the script work only on one output like if I try to comment on the other one and press ENTER it wont work....

推荐答案

您必须知道将仅检索PHP文件的最后输出.所以我想知道您在PHP文件中输出了什么?您可以张贴PHP代码吗? (很抱歉,在这里回答,但没有足够的要评论的地方).

You must know that only the last output of the PHP file will be retrieved. So I'm wondering what did you output in the PHP file ? Can you post the PHP code please ? (Sorry, for answering here but not enough points to comment).

这篇关于jQuery将最后一个输出作为值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆