基于onkeyup textarea创建onclick按钮

时间:2015-03-20 11:20:17

标签: javascript jquery

我有一个带有onclick功能的te​​xtarea注册评论,昨天我意识到移动用户无法发表评论,因为他们没有在他们的手机键盘上按“Enter”键,所以我试图创建一个按钮,以便移动用户也可以发表评论。但这是我的问题,我试图复制已放置在textarea上的onkeyup代码,默认情况下将onkeyup更改为onclick。事情是我正在尝试创建的新按钮不起作用

以下是使用Enter按钮的评论帖的默认代码:

<textarea class="auto-grow-input" name="text" placeholder="<?php echo $lang['comment_textarea_label']; ?>" data-placeholder="<?php echo $lang['comment_textarea_label']; ?>" data-height="24" onkeyup="SK_registerComment(this.value,<?php echo $sk['input']['post']['id']; ?>,<?php echo $sk['input']['timeline']['id']; ?>,event);">
    <?php echo $lang['comment_textarea_label']; ?>
</textarea>

这是我正在尝试创建的按钮:

<button class="commentButton" onClick="SK_registerComment(this.value,<?php echo $sk['input']['post']['id']; ?>,<?php echo $sk['input']['timeline']['id']; ?>,event);">
    <?php echo $lang['comment_button']; ?>
</button>

====更新====

这是SK_registerComment函数

// Post comment
function SK_registerComment(text, post_id, timeline_id, event) {
    if (event.keyCode == 13 && event.shiftKey == 0) {
        main_wrapper = $('.story_' + post_id);
        comment_textarea = main_wrapper.find('.comment-textarea');
        textarea_wrapper = comment_textarea.find('textarea');
        textarea_wrapper.val('');

        SK_progressIconLoader(comment_textarea);

        $.post(SK_source() + '?t=post&a=comment&post_id=' + post_id, {text: text, timeline_id: timeline_id}, function (data) {

            if (data.status == 200) {
                main_wrapper.find('.comment-wrapper:last').before(data.html);
                main_wrapper.find('.story-comment-activity').html(data.activity_html);
            }

            SK_progressIconLoader(comment_textarea);
        });
    }
}

2 个答案:

答案 0 :(得分:0)

this.value将是按钮的值,而不是textarea的值。请尝试使用document.getElementsByName("text")[0].innerHTML

答案 1 :(得分:0)

最好的方法是从params中删除this.value并使用document.getElementsByName直接获取值:&#39; text&#39;)[0] .value

 <button class="commentButton" onClick="SK_registerComment(<?php echo $sk['input']['post']['id']; ?>,<?php echo $sk['input']['timeline']['id']; ?>,event);">
<?php echo $lang['comment_button']; ?>
</button>

function SK_registerComment(postid,timelineid,event){
   value = document.getElementsByName('text')[0].value;
   // rest of your code here
}

function SK_registerComment(post_id, timeline_id, event) {

  text = document.getElementsByName('text')[0].value;   // using jquery $('textarea[name="text"]').val();
  alert(text);
  if (event.keyCode == 13 && event.shiftKey == 0) {
    main_wrapper = $('.story_' + post_id);
    comment_textarea = main_wrapper.find('.comment-textarea');
    textarea_wrapper = comment_textarea.find('textarea');
    textarea_wrapper.val('');

    SK_progressIconLoader(comment_textarea);

    $.post(SK_source() + '?t=post&a=comment&post_id=' + post_id, {text: text, timeline_id: timeline_id}, function (data) {

        if (data.status == 200) {
            main_wrapper.find('.comment-wrapper:last').before(data.html);
            main_wrapper.find('.story-comment-activity').html(data.activity_html);
        }

        SK_progressIconLoader(comment_textarea);
    });
  }
}
相关问题