-1

如何使用 jQuery 作为文本框中的用户类型替换文本(在本例中为“帐户编号”)?我想用在文本框中输入的数字替换文本“”。

<input id="accoutnnumber" name="accoutnnumber" type="text">

<div id="divid">
    This is the Account: <replace with accountnumber>
</div>

<p id="divid">
    On my account (# <replace with accountnumber>) ......
</p>
4

3 回答 3

2

你可以做:

$("#accoutnnumber").keypress(function() {
    $("#divid").text(this.value);
});

此外,ID必须是唯一的。

于 2013-08-16T18:40:34.760 回答
1

您需要具有相同类的元素,包含帐号:

<input id="accountnumber" name="accountnumber" type="text">

<div id="divid">
  This is the Account: <span class="replaced"></span>
</div>

<p>
    On my account (<span class="replaced"></span>) ......
</p>

填写这些keyupchange(以便通过鼠标等粘贴仍会触发更新):

$('#accountnumber').on('keyup change input',
  function() {
    $('.replaced').text($(this).val());
  }
 );

示例:http ://codepen.io/paulroub/pen/ofHux

于 2013-08-16T18:45:56.750 回答
0

http://jsfiddle.net/dUgnk/

为了便于阅读,我稍微修改了您的代码。此外,ID 必须是唯一的,所以我也切换了它。

HTML:

<input id="accountNumber" name="accountNumber" type="text">


<div id="divOne">
    This is the Account:
</div>

<div id="divTwo">
    On my Account: 
</p>

JavaScript:

$("#accountNumber").keypress(function() {
    $("#divOne").text('This is the Account: ' + this.value);
    $("#divTwo").text('On my Account: ' + this.value);
});
于 2013-08-16T18:46:47.053 回答