用户按文本框上的任意键时访问父Div ID

时间:2015-11-07 11:16:54

标签: javascript jquery

  

每当用户按下文本框中的任意键时,我想访问父div的data-user-id ...

     

类面板只是一个聊天窗口,其中包含动态分配给他的接收者ID。

HTML:

<div class="chat-window-container">
            <div class="panel panel-default container" data-user-id="a4c2c119-623e-436d-a750-a521012953fb" id="chat-0001" style="display: block;">
                    <div class="panel-heading" data-toggle="chat-collapse" data-target="#chat-bill">
                        <a href="#" class="close"><i class="fa fa-times"></i></a>
                        <a href="#">
                            <span class="pull-left">
                                <img src="../../Images/bluemanmxxl.png" width="40">
                            </span>
                            <span class="contact-name">ser fdf</span>
                        </a>
                    </div>

                    <div class="panel-body" id="chat-bill" style="height:300px;overflow-y:scroll">
                    <div class="panel-body" id="chat-bill">
                        <div class="media">
                            <div id="chatContainer">
                            </div>
                        </div>
                        <input type="text" id="msg" class="form-control ChatText" onkeydown="return sendfunction(event)" placeholder="Type message...">
                    </div>

                    </div>
            </div>

             <div class="panel panel-default container" data-user-id="7c344592-f323-4baa-869c-a5210129041e" id="chat-0002" style="display: block;">

                    <div class="panel-heading" data-toggle="chat-collapse" data-target="#chat-bill">
                        <a href="#" class="close"><i class="fa fa-times"></i></a>
                        <a href="#">
                            <span class="pull-left">
                                <img src="../../Images/bluemanmxxl.png" width="40">
                            </span>
                            <span class="contact-name">ABC sdsd</span>
                        </a>
                    </div>


                        <div class="media">
                            <div id="chatContainer">
                            </div>
                        </div>
                        <input type="text" id="msg" class="form-control ChatText" onkeydown="return sendfunction(event)" placeholder="Type message...">
            </div>
        </div>

JS:

function sendfunction(e) { 
    var keynum;

    if (window.event) { 
        keynum = e.keyCode; 
    } 
    if (keynum == 13) { 
        strChatText = $("#msg").val(); 
        if (strChatText != '') { 
            var strGroupName = gname; 
            if (typeof strGroupName !== 'undefined' && strGroupName !== false) 
                chat.server.send(strChatText, gname, '@ViewBag.LoginUserId', a); 
            $("#msg").val(''); 
        } 
        return false; 
    } 
}

2 个答案:

答案 0 :(得分:1)

<强> Working fiddle

由于id属性在同一文档中应该是唯一的,因此将id=msg的{​​{1}}更改为唯一。

以下行应该做的工作:

input

Hpe有帮助。

答案 1 :(得分:0)

$(e.target)。家长(&#39; .container&#39)。ATTR(&#39;数据用户ID&#39)