使用表单按钮填充文本输入

Populate Text Input With Form Buttons

本文关键字:文本 输入 填充 按钮 表单      更新时间:2023-09-26

我有一个简单的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);
});