简体   繁体   English

JavaScript函数仅在首次调用时起作用

[英]Javascript function only works on the first call

I'm developing a Question&Answer website in php and I want to print the answer comments when I press the button. 我正在用php开发一个Question&Answer网站,当我按下按钮时,我想打印答案注释。 Everything works like a charm but only on the first button. 一切都像一个咒语,但仅在第一个按钮上起作用。 I have an idea why this does't work, I guess it only takes into account the first id that it finds. 我有一个想法,为什么它不起作用,我想它仅考虑到它找到的第一个ID。

So , my question is, is there any way to name the element I want to call based on its id? 所以,我的问题是,有什么方法可以根据其ID命名要调用的元素吗? For example: 例如:

<button class="btn icon-chat" title="Add a comment on this answer"
                        type="button" id="showarea . {answer['answerid']"} name="showarea" value="Show Textarea">
                    Comment</button>
<div id="textarea">
                    {include file="comment_form.tpl"}
                </div>

But how would I call this PHP variable on my JS function? 但是,如何在我的JS函数上调用此PHP变量?

$("#textarea, #textarea-ok").hide(); // or you can have hidden w/ CSS
$("#showarea").click(function(){
    $("#textarea").show();
});
$("#textarea-ok, #cancel").click(function(){
    $("#textarea").hide();
});

Is this the best approach? 这是最好的方法吗? Any advise regarding to the JS code you can give? 关于您可以提供的JS代码有什么建议吗?

Kind Regards 亲切的问候

Live method should be ok 现场方法应该可以

$("body").on("click", ".myClass", function(){
    // do it again // or #myId
});

Don't forget about an event with an Id selector can be only on one element, and class on every one... 不要忘了带有ID选择器的事件只能在一个元素上,而类在每个元素上...

Edit with example 用示例编辑

<div class="post-button clearfix">
    // i changed this button as well
    <button class="btn icon-chat show-textarea" title="Add a comment on this answer" type="button" data-answer="{$answer['publicationid']}">Comment</button>

    <div class="textarea">
        {include file="comment_form.tpl"}
    </div>
</div>


// comment_form.tpl

// i added a master container
<div class="comment-form">
    <form method="post" action="{$BASE_URL}controller/actions/comments/create_comment.php">
        <textarea name="comment" rows="4" cols="40" class="qa-form-tall-text"></textarea>

        // i deleted the wrong input here
        <input type="hidden" name="answerid" value="{$answer['answerid']}" />
        <input type="hidden" name="questionid" value="{$question['publicationid']}" />

        // i changed these 2 buttons as well
        <button type="button" class="textarea-cancel qa-form-tall-button qa-form-tall-button-comment">Cancel</button>
        <button type="submit" class="textarea-ok">Ok</button>
    </form>
</div>

Then you change the script with class in selector like : 然后,您可以使用选择器中的类更改脚本,例如:

...

$('.comment-form').hide();

$("body").on("click", ".show-textarea", function(){
    $('.comment-form').show();
});

$("body").on("click", ".textarea-ok, .textarea-cancel", function(){
    $('.comment-form').hide();
});

....

More about Jquery Selector : https://www.w3schools.com/jquery/jquery_ref_selectors.asp 有关Jquery选择器的更多信息: https : //www.w3schools.com/jquery/jquery_ref_selectors.asp

More about live method wit .on() : https://www.w3schools.com/jquery/event_on.asp 有关实时方法wit .on()的更多信息: https ://www.w3schools.com/jquery/event_on.asp

More about Html forms https://www.w3schools.com/html/html_forms.asp 有关HTML表单的更多信息https://www.w3schools.com/html/html_forms.asp

Read these docs to be ok with yourself ;) 阅读这些文档,以使自己一切都好;)

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

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