0

After text is entered in the textarea i would like to target this class to let the user submit by pressing enter.

cmnt += '<td class="cmnt_save" data-id="' + id + '">Save Comment</td>';

I would like to Target the Save comment td class using jquery

Everything I've tried has failed miserably.. i've tried using jquery, JS and some inline JS too.

Here's my function with my tables:

// This function retrieves comments
function get_comments(id ,afterCtl ,ctl) {
    var cmnt = '';

show_loading('.loading_box');

// Create the new row in the database - ajax returns json version.
var add_url = '/ajax/project_comment_list.php?id=' + id;

var slot = $.getJSON(add_url)
    .done(function(data) {
        // Draw the entire comment-block then display it

        cmnt = '<tr class="cmnts">';
        cmnt += '    <td colspan="11">';
        cmnt += '        <table>';
        cmnt += '            <tr class="cmnt_add">';
        cmnt += '                <td class="cmnt_text">'
        cmnt += '                    <input type="text" id="cmnt_new" name="cmnt_new" rows="1">';
        //cmnt += '                    <textarea id="cmnt_new" name="cmnt_new" rows="1"></textarea>';
        cmnt += '                </td>';
        cmnt += '                <td></td>';
        cmnt += '                <td></td>';
        cmnt += '                <td class="cmnt_save" data-id="' + id + '">Save Comment</td>';
        cmnt += '            </tr>';

        $.each(data.ProjectComments, function (index, value) {
            cmnt += add_comment_row(value.TEXT ,blank_if_undefined(value.EMP_INIT) ,value.ENTER_DATE);
        });

        cmnt += '        </table>';
        cmnt += '    </td>';
        cmnt += '</tr>';

        remove_comments(ctl);

        $(afterCtl).after(cmnt);
        $('#cmnt_new').focus();

        $(ctl).addClass('cmnt_active');
        $(ctl).html('&nbsp;-&nbsp;');

        //register the event
        $('td.cmnt_save').click(function(){
            add_comment($(this));
        });

        comment_shown = id;
    })
    .fail(function(jqxhr, status, error) {
        hide_loading('.loading_box');
        show_error('There was an error pulling the comments.');
    });

    //wire up the event
    $('textarea#cmnt_new').keydown(function(e) {
        if(e.which == 13) {
            e.preventDefault();
            alert('Cripes!  You pressed enter!');
        }
    })

}

If anyone could be of any help to get this task accomplished or just recommend other/better ways of making this function correctly. Thanks in advance for the help.

4

3 回答 3

1

如果我对您的理解正确,您只想保存用户在相关文本区域中按回车键后添加的评论。

我想你想要这样的东西:http: //jsfiddle.net/5JKy8/2/

HTML

<table id="comments">
    <tr>
        <th>Name</th>
        <th>Comment</th>
    </tr>
    <tr>
        <td>Foo</td>
        <td><textarea class="comment" rows="1" id="333"></textarea></td>
    </tr>
    <tr>
        <td>Bar</td>
        <td><textarea class="comment" rows="1" id="666"></textarea></td>
    </tr>                
</table>

Javascript

$(document).ready( function() {

    $("body").on("keydown",".comment", function(e) {
        if(e.which == 13) {            
            var comment = $(this).val(),
                id = $(this).data("id");

            saveComment(comment,id).done( function() {
                $(this).parent("td").text(comment);
            });
        }
    });    

});

function saveComment(comment,id) {
    return $.ajax({
        url: 'yourScript.php',
        data: {comment:comment,id:id}
    });
}

注意:在 jsfiddle 中, saveComment() 返回一个布尔值用于演示目的,但只需使用注释掉的代码。

于 2013-11-06T05:28:20.123 回答
1

文本字段的 ID 是什么..? #cmnt_new?

$('#static-parent').on('keyup', '#cmnt_new', function(ev){ 
    if(e.keyCode == 13)
    {
        // do whatever here
    }
});

#static-parent不是动态加载并且在其中#cmnt_new的东西(父级)在哪里。我相信你甚至可以做到$(document).on(..。要让脚本处理动态加载的内容,您必须使用委托并在页面上处理静态内容。

于 2013-11-06T00:46:20.257 回答
0

使用事件对象的keyCode属性onkeyupevent.keyCode === 13对于输入按钮,例如:

$('#cmnt_new').keyup(function(e){
  if(e.keyCode === 13){
    // do your thing here
  }
});

请注意,您已将<input type='text' />其与</textarea>.

于 2013-11-06T00:24:07.497 回答