0

сама форма

<form action="index.php" name="searchform" method=get>
    <input type="text" id="auto1" name="searchstring" size="20" value="{$searchstring|default:""}">
</form>

отправка формы

<a id="aaa" onClick="First();" href="">отправить</a>

функция

<script type="text/javascript">
    function First () {
    var url = 'index.php?searchstring=' + title;
    aaa.href =  url;
    }
</script>

Как по нажатию на клавишу enter её отправить? а если адаптировать эту функцию под button, то будет отправляться эта форма по клавише?

2 ответа 2

1

function submitForm() {
  alert('before submit');
  document.getElementById('frm').submit();
}

document.getElementById('auto1').onkeypress = function(event) {
  if (event.keyCode == 13) {
    submitForm();
  }
}
<form action="index.php" name="searchform" method="get" id="frm">
  <input type="text" id="auto1" name="searchstring" size="20" value="{$searchstring|default:" "}">
</form>

<a onclick="submitForm()" href="javascript://">отправить</a>

9
  • что-то не отправляется...точнее не реагирует 14 ноя 2016 в 14:45
  • @sbaikov Вы нажали Enter, когда фокус был на input?
    – user199303
    14 ноя 2016 в 14:47
  • да, курсор был на input 14 ноя 2016 в 14:49
  • @sbaikov JavaScript был ниже input'a?
    – user199303
    14 ноя 2016 в 14:51
  • да, пробовал и выше и ниже поставить 14 ноя 2016 в 14:52
1

Добавить кнопку <input type="submit"> или <button type="submit"></button> МОЖНО СКРЫТУЮ

<form action="index.php" name="searchform" method=get>
    <input type="text" id="auto1" name="searchstring" size="20" value="{$searchstring|default:""}">
<input type="submit" style='display: none;'>
</form>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.