0

我试图在 joomla 模块内的表单中禁用 enter 按键,但我无法让它工作......这是代码。

<script type="text/javascript">
   function stopRKey(evt) {
      var evt = (evt) ? evt : ((event) ? event : null);
      var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
      if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
   }

     document.onkeypress = stopRKey;
</script> 

<form id="searchbox" action="<?php echo JRoute::_('index.php'); ?>"                           method="post"         role="search">
    <input type="text" value="" name="searchword" placeholder="<?php echo JText::_('TPL_WARP_SEARCH'); ?>" />
    <button type="reset" value="Reset"></button>
    <input type="hidden" name="task"   value="search" />
    <input type="hidden" name="option" value="com_search" />
    <input type="hidden" name="Itemid" value="<?php echo $itemid > 0 ? $itemid : JRequest::getInt('Itemid'); ?>" /> 
</form>

<script src="<?php echo $warp['path']->    url('js:search.js'); ?>"></script>

<script>
    jQuery(function($) {
       $('#searchbox input[name=searchword]').search({'url': '<?php echo      JRoute::_("index.php?option=com_search&tmpl=raw&type=json&ordering=&    searchphrase=all");?>', 'param': 'searchword', 'msgResultsHeader': '<?php echo   JText::_("TPL_WARP_SEARCH_RESULTS"); ?>', 'msgMoreResults': '<?php echo JText::_("TPL_WARP_SEARCH_MORE"); ?>', 'msgNoResults': '<?php echo JText::_("TPL_WARP_SEARCH_NO_RESULTS"); ?>'}).placeholder();
    });
</script>

我尝试了不同的脚本,但到目前为止没有运气......

4

3 回答 3

0

Use event.preventDefault() it prevent default action of the event.

http://api.jquery.com/event.preventDefault/

于 2013-08-23T10:24:38.383 回答
0
("input").live("keypress", function(e) {
        if (e.keyCode == 13) {
            event.preventDefault();
            return false; // prevent the button click from happening event.preventDefault() or return false, either one is enough
        }
});
于 2013-08-23T10:25:23.430 回答
0

在 html 搜索按钮中更改它:

<input type="text" class="searchButton" value="" name="searchword" placeholder="<?php echo JText::_('TPL_WARP_SEARCH'); ?>" />

然后在脚本中:

$(".searchButton").click(e){
    if (e.keyCode == 13) {
        return false; // prevent the button click from happening
        e.preventDefault(); // prevent default html form submit
    }
}
于 2013-08-23T10:28:44.863 回答