使输入键功能像tab键

时间:2016-05-27 07:26:42

标签: javascript php key enter

朋友们,我知道这个问题已经有很多解决方案了,我已经使用了一个,但由于某些未知的原因,它在我的应用程序中无效。所以请帮帮我,让我知道我哪里出错了。请不要把这个问题标记为重复。我真的需要你的帮助。 提前致谢。 这是我尝试过的。

// Catch the keydown for the entire document
$(document).keydown(function(e) {

  // Set self as the current item in focus
  var self = $(':focus'),
      // Set the form by the current item in focus
      form = self.parents('form:eq(0)'),
      focusable;

  // Array of Indexable/Tab-able items
  focusable = form.find('input,a,select,button,textarea,div[contenteditable=true]').filter(':visible');

  function enterKey(){
    if (e.which === 13 && !self.is('textarea,div[contenteditable=true]')) { // [Enter] key

      // If not a regular hyperlink/button/textarea
      if ($.inArray(self, focusable) && (!self.is('a,button'))){
        // Then prevent the default [Enter] key behaviour from submitting the form
        e.preventDefault();
      } // Otherwise follow the link/button as by design, or put new line in textarea

      // Focus on the next item (either previous or next depending on shift)
      focusable.eq(focusable.index(self) + (e.shiftKey ? -1 : 1)).focus();

      return false;
    }
  }
  // We need to capture the [Shift] key and check the [Enter] key either way.
  if (e.shiftKey) { enterKey() } else { enterKey() }
});

html代码 -

<form id="purchase" action="p_senddata.php" method="POST" >
   Bill_no:<input type="text" name="p_billno" id="pbillno"    /><br />
   Date:<input type="text" name="p_date" id="pdate"  /><br />
   Name of party:<input type="text" name="p_nameofparty" id="pnameofparty" /><br /><br />

<input type="submit">
</form>

0 个答案:

没有答案