Jump to content
  • 0

Привязка кнопки веб-эелемента к кнопке на клавиатуре


Warlock
 Share

Question

Такой вопрос, можно ли кнопку в калькуляторе, привязать к кнопке клавиатуры. К примеру, что бы при нажатии на кнопку равно или ввод на клавиатуре, калькулятор показывал бы результат, а не выводил бы равно в поле для ввода или не очищал поле для ввода?

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

А вот здесь, это куда можно поставить?

<table border="1" cellspacing="12">
<tr>
<td>
<center><h1>Калькулятор</h1></center>
<table border="0" cellspacing="12">
<tr>
<td>
<form name="Calculator">
<input type="text" name="answer" size="45" value></td>
</tr>
</table>
<center><table border="0" cellspacing="12">
<tr valign="top">
<td><input type="button" name="times" size="200" value=" * " onClick="document.Calculator.answer.value += '*'"></td>
<td><input type="button" name="divided" size="200" value=" / " onClick="document.Calculator.answer.value += '/'"></td>
<td><input type="button" name="minus" size="200" value=" - " onClick="document.Calculator.answer.value += '-'"></td>
<td><input type="button" name="minus" size="200" value=" C " onClick="document.Calculator.answer.value = ''"></td>
</tr>
<tr valign="top">
<td><input type="button" name="seven" size="200" value=" 7 " onClick="document.Calculator.answer.value += '7'"></td>
<td><input type="button" name="eight" size="200" value=" 8 " onClick="document.Calculator.answer.value += '8'"></td>
<td><input type="button" name="nine" size="200" value=" 9 " onClick="document.Calculator.answer.value += '9'"></td>
<td><input type="button" name="zero" size="200" value=" 0 " onClick="document.Calculator.answer.value += '0'"></td>
</tr>
<tr>
<td><input type="button" name="four" size="200" value=" 4 " onClick="document.Calculator.answer.value += '4'"></td>
<td><input type="button" name="five" size="200" value=" 5 " onClick="document.Calculator.answer.value += '5'"></td>
<td><input type="button" name="six" size="200" value=" 6 " onClick="document.Calculator.answer.value += '6'"></td>
<td><input type="button" name="equals" size="200" value=" = " onClick="document.Calculator.answer.value = eval(document.Calculator.answer.value)"></td>
</tr>
<tr>
<td><input type="button" name="one" size="200" value=" 1 " onClick="document.Calculator.answer.value += '1'"></td>
<td><input type="button" name="two" size="200" value=" 2 " onClick="document.Calculator.answer.value += '2'"></td>
<td><input type="button" name="three" size="200" value=" 3 " onClick="document.Calculator.answer.value += '3'"></td>
<td><input type="button" name="plus" size="200" value=" + " onClick="document.Calculator.answer.value += '+'"></td>
</tr>
</table></center>
</td>
</tr>
</table>

Link to comment
Share on other sites

  • 0

Конкретно здесь никуда. Да и вообще код в том виде, в котором я его написал не применим никуда. Это просто пример работы с событиями. Т.е. я просто показал в какую сторону копать.

Читайте литературу, гуглите, ищите ответ в общем. Готового решения я вам не дам, по крайней мере забесплатно. А вот помочь помогу, но для этого вы должны хоть что-то сами сделать.

Link to comment
Share on other sites

  • 0

Вот попробовал так, но к ответу он добавляет равно, подскажите пожалуйста, что нужно исправить, чтобы не писал равно после вывода ответа, и как сделать, что б и при нажатии на кнопку энтер выводил ответ?

script language="JavaScript">
<!-- Begin
if (document.addEventListener)
{
document.addEventListener("keypress",press,true);
}
else if (document.attachEvent)
{
document.attachEvent("onkeypress",press)
}
function press(e)
{
if (e.keyCode==61)
document.Calculator.answer.value = eval(document.Calculator.answer.value);
}
// End -->
</script>

<center>
<table border="10" cellspacing="12" bordercolor="black" bordercolorlight="black" bordercolordark="yellow">
<tr>
<td>
<form name="Calculator">
<input type="text" name="answer" size="45" value>
</tr>
</table>
<table border="10" cellspacing="12" bordercolor="black" bordercolorlight="black" bordercolordark="red">
<tr valign="top">
<td><input type="button" name="times" size="200" value=" * " onClick="document.Calculator.answer.value += '*'"></td>
<td><input type="button" name="divided" size="200" value=" / " onClick="document.Calculator.answer.value += '/'"></td>
<td><input type="button" name="minus" size="200" value=" - " onClick="document.Calculator.answer.value += '-'"></td>
<td><input type="button" name="minus" size="200" value=" C " onClick="document.Calculator.answer.value = ''"></td>
</tr>
<tr valign="top">
<td><input type="button" name="seven" size="200" value=" 7 " onClick="document.Calculator.answer.value += '7'"></td>
<td><input type="button" name="eight" size="200" value=" 8 " onClick="document.Calculator.answer.value += '8'"></td>
<td><input type="button" name="nine" size="200" value=" 9 " onClick="document.Calculator.answer.value += '9'"></td>
<td><input type="button" name="zero" size="200" value=" 0 " onClick="document.Calculator.answer.value += '0'"></td>
</tr>
<tr>
<td><input type="button" name="four" size="200" value=" 4 " onClick="document.Calculator.answer.value += '4'"></td>
<td><input type="button" name="five" size="200" value=" 5 " onClick="document.Calculator.answer.value += '5'"></td>
<td><input type="button" name="six" size="200" value=" 6 " onClick="document.Calculator.answer.value += '6'"></td>
<td><input type="button" name="equals" size="200" value=" = " onClick="document.Calculator.answer.value = eval(document.Calculator.answer.value)"></td>
</tr>
<tr>
<td><input type="button" name="one" size="200" value=" 1 " onClick="document.Calculator.answer.value += '1'"></td>
<td><input type="button" name="two" size="200" value=" 2 " onClick="document.Calculator.answer.value += '2'"></td>
<td><input type="button" name="three" size="200" value=" 3 " onClick="document.Calculator.answer.value += '3'"></td>
<td><input type="button" name="plus" size="200" value=" + " onClick="document.Calculator.answer.value += '+'"></td>
</tr>
</table>
</center>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. See more about our Guidelines and Privacy Policy