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

时间:2015-02-01 02:48:26

标签: javascript html forms

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

3 个答案:

答案 0 :(得分:1)

如果您有一些选择器可以选择包含这些段落的所有<p>标记,您可以执行以下操作:

&#13;
&#13;
$(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);
  });
});
&#13;
.prefill-sentence {
  margin-left: 20px;
}
&#13;
<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>
&#13;
&#13;
&#13;

(注意我假设您的名称为&#34; desc&#34;用于您的描述输入。理想情况下,您可以使用类或ID在实际代码中更轻松地定位它。)

答案 1 :(得分:1)

这是你在找什么?

http://plnkr.co/edit/S3OegSh80UH6oQJPDatr?p=preview

&#13;
&#13;
$(function(){
  $('p').each(function(){
      $(this).after('<button>Click<\/button>');    
    });
    
    $('button').on('click', function(){
      var txt = $(this).prev().text();
      $('input').eq(0).val(txt);
    })
});
&#13;
&#13;
&#13;

答案 2 :(得分:1)

你可能想要为那些php生成的段落/ div添加更具体的东西,这样他们就可以安全地被JS选中和操纵了。

CodePen: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);

});