使用表单按钮填充文本输入
Populate Text Input With Form Buttons
我有一个简单的html表单,我在其中输入标题和描述并点击提交。 页面顶部是我经常复制并粘贴到这些字段中的一些文本段落。 这是一项重复性任务,段落是使用 php 动态生成的。
我可以在每个段落或div 的末尾放置一个按钮或链接,用脚本填写我的表单输入字段吗? 然后我所要做的就是点击提交。我也已经在页面上使用 jquery。
编辑:
<p>Sentence one. Longer than this</p><!--would like a button here to populate field in form below-->
<p>Sentence two. Longer than this</p>
<p>Sentence three. Longer than this</p>
<form id="sampleform" action="actionpage.php" method="post">
Title<input type="text" name="title>
Desc<input type="text" name="title>
<input type="submit" value="Submit"></form>
如果你有一些选择器可以选择包含这些段落的所有<p>
标签,你可以执行以下操作:
$(function() {
var $descInput = $('input[name=desc]');
// wrap the text of each paragraph in a span so we can target it easily.
// Then add a button inside each <p> at the end that will prepopulate that text.
$('p.prefill').wrapInner('<span class="text"></span>').append('<button class="prefill-sentence">Prefill</button>');
// Add a click handler for all the newly added buttons
$('button.prefill-sentence').click(function() {
// get the contents of the span we used to wrap the sentence with
var sentence = $(this).prev('.text').text();
// add that sentence to the current value of the description input
$descInput.val($descInput.val() + sentence);
});
});
.prefill-sentence {
margin-left: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="prefill">Sentence one. Longer than this</p>
<p class="prefill">Sentence two. Longer than this</p>
<p class="prefill">Sentence three. Longer than this</p>
<form id="sampleform" action="actionpage.php" method="post">
Title
<input type="text" name="title" />Desc
<input type="text" name="desc" />
<input type="submit" value="Submit" />
</form>
(注意,我假设您的描述输入有一个名称"desc"。理想情况下,您可以使用类或 id 在实际代码中更轻松地定位它)。
这是你要找的吗?
http://plnkr.co/edit/S3OegSh80UH6oQJPDatr?p=preview
$(function(){
$('p').each(function(){
$(this).after('<button>Click<'/button>');
});
$('button').on('click', function(){
var txt = $(this).prev().text();
$('input').eq(0).val(txt);
})
});
你可能想为那些 php 生成的段落/div 添加更具体的东西,以便 JS 可以安全地选择和操作它们。
代码笔: http://codepen.io/anon/pen/PwJwmO
.HTML
<div class="text-section">
Sentence one. Longer than this
</div>
<div class="text-section">
Sentence two. Longer than this
</div>
<div class="text-section">
Sentence three. Longer than this
</div>
<form id="sampleform" action="actionpage.php" method="post">
Title<input type="text" name="title">
Desc<input type="text" name="desc">
<input type="submit" value="Submit">
</form>
.JS
var $text_section = $('.text-section');
var $description_field = $('input[name="desc"]');
$text_section.each(function(){
var section_text = $(this).text();
var $autofill_button = $('<button>Autofill</button>');
$autofill_button.click(function(){
$description_field.val(section_text);
});
$(this).append($autofill_button);
});
相关文章:
- 让文本输入幻灯片显示输入时的新文本输入?然后向后滑动
- Sails.js:同时发布文本输入和一个文件
- 来自文本输入null的html javascript变量
- 当没有文本输入聚焦时检测空格键按下
- JS中的按钮和文本输入
- JavaScript:在调用函数的文本输入上按enter键
- jQuery自动完成功能不适用于多个文本输入
- 使用JS从选择和文本输入中捕获值,并将输出返回到HTML
- 如何通过jQuery发送选定的元素和文本输入
- HTML如何根据javascript函数的返回值限制文本输入
- jquery插件或javascript方法自动调整文本输入(而非文本区域)(固定宽度)可变高度的大小
- 在jquery中获取文本输入val始终为空
- HTML文本输入大小
- 文本输入与文本区域
- 角度ui选择标记模糊时丢失文本输入
- 文本输入值中的JavaScript变量
- Angular指令中的最佳实践是将文本输入设置为英尺和英寸的格式
- 如何在不通过模型验证的情况下屏蔽文本输入中输入的字符
- 在文本输入区域中创建双向更新
- 不使用自定义CSS或HTML(使用框架方法)的角度材质文本输入或文本区域标签大小