通过单击带有JQuery的链接来填写输入字段
问题描述:
某人如何通过单击jQuery使用jquery在输入字段中填写一些文本?
How might someone fill in an input field with some text using jquery by clicking that text?
文本是动态生成的,因此无法提前知道该值.
The text is generated dynamically so the value is not know ahead of time.
例如:
<input id="a_input_id" type="text">
<a href="#" class="special_field_link">@r.SpecialField<a>
当某人单击链接文本字段的链接时,将填充@r.SpecialField
When someone clicks that link the text field with be populated with the value of @r.SpecialField
答
HTML:
<input id="a_input_id" type="text">
<a href="#" class="special_field_link">@r.SpecialField<a>
JS:
$(function(){
$('.special_field_link').live('click', function() {
$("#a_input_id").val($(this).html());
});
});
此外,您可以使用.text()代替此答案中列出的.html():
Also, you can use .text() instead of .html() as listed in this answer: Fill in Input Field by Clicking Link with JQuery