javascript - 在文本字段中显示按钮的值

标签 javascript jquery html

please refer this jsfiddle

$("#dialog").dialog({
  autoOpen: false
});
$("#opener").click(function() {
  $("#dialog").dialog("open");
});
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>dialog demo</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.12.4.js"></script>
  <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>

<body>

  <button id="opener">open the dialog</button>
  <div id="dialog" title="Phone Number">
    <input type="button" value="1">
    <input type="button" value="2">&nbsp;<input type="button" value="3"><br />
    <input type="button" value="4">&nbsp;<input type="button" value="5">&nbsp;<input type="button" value="6"><br />
    <input type="button" value="7">&nbsp;<input type="button" value="8">&nbsp;<input type="button" value="9"><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="button" value="0">
  </div>
  <input type="text" placeholder="phone number">

</body>

</html>

In this fiddle link if i click on every button number its value should reflect on text field how to do it?? any suggestions please

最佳答案

我建议你改变两件事。首先为您的电话号码字段提供 ID“电话”,如下所示:

<input id="phone" type="text" placeholder="phone number">

其次,添加一个脚本,将按下的按钮的值添加到您的输入值,如下所示:

$( '#dialog input').click (function() {
    $("#phone").val( $("#phone").val() + $(this).val());
})

另请参阅:https://jsfiddle.net/52ec3nzn/7/ ,或使用此内联脚本来测试解决方案:

$(function() {
  $( "#dialog" ).dialog({ autoOpen: false });
  $( "#opener" ).click(function() {
    $( "#dialog" ).dialog( "open" );
  });

  $( '#dialog input').click (function() {
    $("#phone").val( $("#phone").val() + $(this).val());
  });
});
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>dialog demo</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.12.4.js"></script>
  <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
 
<button id="opener">open the dialog</button>
<div id="dialog" title="Phone Number">
  <input type="button" value="1">
       <input type="button" value="2">&nbsp;<input type="button" value="3"><br />
       <input type="button" value="4">&nbsp;<input type="button" value="5">&nbsp;<input type="button" value="6"><br />
       <input type="button" value="7">&nbsp;<input type="button" value="8">&nbsp;<input type="button" value="9"><br />
       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="0">
</div>
<input id="phone" type="text" placeholder="phone number">
 
</body>
</html>

关于javascript - 在文本字段中显示按钮的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49170324/

相关文章:

javascript - 简单的javascript问题

javascript - 如何生成带有样式的PDF并将其上传到服务器(Jquery)

javascript - jquery iframe 链接到新的 div/tab/window?

html - 考试 70-480 : CSS order of anchor elements

php - PHP 中的原型(prototype)继承(类似于 JavaScript)

javascript - jQuery 加载与构建元素

javascript - 如何获取父节点的子节点并使用 jquery ajax 处理它

javascript - jQuery 同位素表问题,Firefox 除外

javascript - Lodash、Underscore.js 还是 Lazy.js?

javascript - 如何从插件外部调用插件的函数