0 レビュー
1 回答
php-Javascript関数は最初の呼び出しでのみ機能します
PHPでQuesty&Answer Webサイトを開発していて、ボタンを押したときに回答コメントを印刷したいと思っています。すべてが魅力のように機能しますが、最初のボタンでのみ機能します。これが機能しない理由がわかります。最初に見つかったIDのみが考慮されると思います。
それで、私の質問は、IDに基づいて呼び出したい要素に名前を付ける方法はありますか?例:
<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>
しかし、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();
});
これが最善のアプローチですか?提供できるJSコードに関するアドバイスはありますか?
よろしく
わからない
0
レビュー
答え :
解決策:
ライブメソッドは問題ないはずです
$("body").on("click", ".myClass", function(){
// do it again // or #myId
});
Idセレクターを使用するイベントは、1つの要素にのみ存在し、すべての要素にクラスを設定できることを忘れないでください...
例を使用して編集
<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>
次に、セレクターのクラスを使用してスクリプトを次のように変更します:
...
$('.comment-form').hide();
$("body").on("click", ".show-textarea", function(){
$('.comment-form').show();
});
$("body").on("click", ".textarea-ok, .textarea-cancel", function(){
$('.comment-form').hide();
});
....
Jquery Selectorの詳細: https://www.w3schools.com/jquery/jquery_ref_selectors.asp
ライブメソッドwit.on()の詳細: https://www.w3schools.com/jquery/event_on.asp
Htmlフォームの詳細 https://www.w3schools.com/html/html_forms.asp
これらのドキュメントを読んで、自分で問題がないことを確認してください;)
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。