使用JS将文本添加到Rails中的表单

时间:2011-09-21 11:33:58

标签: javascript ruby-on-rails

当我按下键盘上的链接时,我在使用javascript更新文本框时遇到问题。谁能告诉我哪里出错了?

继承人我的HTML:

<div class="keypad_title">
  <h2>Login to Begin</h2>
</div>
<div class="keypad">
  <div class="keypad_input">
<%= form_tag :html => {:name => "key_pad_form"} do%>
  <%= text_field_tag :keypad_input, "", :id => "keypad_box" %>
<% end %>
</div>
  <table id="keypad">
<tr>
  <td><a href="#" class="gray square button">1</a></td>
  <td><a href="#" class="gray square button">2</a></td>
  <td><a href="#" class="gray square button">3</a></td>
</tr>
<tr>
  <td><a href="#" class="gray square button">4</a></td>
  <td><a href="#" class="gray square button">5</a></td>
  <td><a href="#" class="gray square button">6</a></td>
</tr>
<tr>
  <td><a href="#" class="gray square button">7</a></td>
  <td><a href="#" class="gray square button">8</a></td>
  <td><a href="#" class="gray square button">9</a></td>
</tr>
<tr>
  <td><a href="#" class="gray square button">0</a></td>
  <td><a href="#" class="gray square button disabled"></a></td>
  <td><a href="#" class="gray square button icons">D</a></td>
</tr>
</table>
<div class="enter">
<a href="#" class="green large button keypad_enter">Enter</a>

  </div>
</div>
<script type="text/javascript" src="../keypad.js"></script>

这是我的Javascript:

$(function(){  
var $write = $('#keypad_box'),  

$('#keypad a').click(function(){  
    var $this = $(this),  
        character = $this.html(); // If it's a lowercase letter, nothing happens to this variable

    // Add the character  
    $write.html($write.html() + character);  
 });  
});

2 个答案:

答案 0 :(得分:0)

您应该使用$(document).ready而不是$(function(){...。我希望它有所帮助。

答案 1 :(得分:0)

使用val()设置值: -

$(function(){  
 var $write = $('#keypad_box');  

$('#keypad a').click(function(){
   var $this = $(this),  
    character = $this.html(); // If it's a lowercase letter, nothing happens to this variable
// Add the character  
$write.val($write.val() + character);  
 });  
});