2011-09-21 47 views
0

我在按下我的小键盘中的链接时正在使用javascript更新文本框时出现问题。谁能告诉我我要去哪里?将文本添加到带有JS的Rails窗体中

继承人我的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); 
}); 
}); 

回答

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); 
}); 
});