我似乎无法弄清楚这个简单的任务。我有一个输入文本字段,我想从中接受一个值,并在单击链接时更改该值。我的 HTML 代码如下:
<body>
<div id="outsideContainer">
<div id="guessContainer">
<h3> Guess a number between 1 and 10 </h3>
<input id="txtfield" name="txtfield" type="text" class="value"/><br>
<a href=" " class="query"> Submit here </a>
</div>
<div id="guessShow">
<p class="hotter hideStuff" > Getting Hotter!! </p>
<p class="colder hideStuff"> Getting Colder, BRRR!!! </p>
<p class="gotIt hideStuff"> Awesome! You have guessed the number </p>
</div>
</div>
</body>
我想要做的是将 class =“value” 标记中显示的文本更改为某些内容。我尝试过使用
jQuery(document).ready(function(){
$('.query').on('click', function(){
$(this).closest('#guessContainer').find('.value').css("value", "whoa");
});
});
但它不起作用。我还尝试使用 .css 方法并为输入分配一个值,但这也不起作用。任何帮助将不胜感激。
最佳答案
尝试使用.attr()
$(this).closest('#guessContainer').find('.value').attr("value", "whoa");
或
$(this).closest('#guessContainer').find('.value').val("whoa");
关于javascript - 如何更改 javascript 中 <input> 标签的文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16967594/