从getElementsByClassName获取ENTRY键按下文本框

时间:2017-01-16 07:56:00

标签: javascript

我有一个文本框和选择元素。

如果有人在文本框中输入了一个输入键,我想触发警报。我编写JS代码来获取文本框,但无法执行触发器操作。我怎么能这样做?



var tab = document.getElementsByClassName('PDSCriteriaSections')[0];
var children = tab.childNodes[0].childNodes[0];
var textbox = children.firstChild.innerHTML;

<table>
  <tr>
    <td class="PDSCriteriaSections">
      <div>
        <ul>
          <li>
            <input type="text">
            <select>
              <option value="MTJobNo">Job No</option>
              <option value="EmptyInDate">EmptyIn Date</option>
              <option value="ContainerNo">Container No</option>
            </select>
            <input value="-" style="display: none;" type="button">
          </li>
        </ul>
      </div>
    </td>
  </tr>
</table>
&#13;
&#13;
&#13;

JSFIDDLE:https://jsfiddle.net/ctpn6mr6/

1 个答案:

答案 0 :(得分:0)

// ...
// textbox redefintion
var textbox = children.firstChild;
textbox.addEventListener("keydown", function(e){
    // 13 is the keyCode of enter button
    if(e.keyCode == 13){
        alert("Enter was pressed");
    }
});