【问题标题】:Populate Text Input With Form Buttons使用表单按钮填充文本输入
【发布时间】:2015-02-01 02:48: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>

【问题讨论】:

  • 你能给我们一些代码来看看吗?
  • 它有我不想分享的信息,但我会准备一个样本。
  • 添加了基本设置。

标签: javascript html forms


【解决方案1】:

如果您有一些选择器可以选择包含这些段落的所有&lt;p&gt; 标签,您可以执行以下操作:

$(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 在实际代码中更轻松地定位它)。

【讨论】:

  • 我什至没有考虑附加文本的按钮,所以我可以使用多个。但这使我的页面更有用。谢谢。
【解决方案2】:

这是你要找的吗?

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);
    })
});

【讨论】:

    【解决方案3】:

    您可能希望为这些 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);
    
    });
    

    【讨论】:

    • 提倡使用全局变量而不使用var不会导致良好的JS习惯。此外,您的按钮不会附加到输入,它们将替换其现有内容。
    • 离开 var 是一个疏忽 - 今天在 PHP 和 JS 之间切换太多,从我阅读问题的方式来看,他似乎希望替换内容而不是附加内容。
    • 这是可以理解的。我只是想确保任何学习你的代码的人都知道省略 var 是一件有风险的事情。
    • 我的意思是要替换文本。但是现在我想起来了,附加对我也很有用。我会看到继续这项重复性的任务,最终会更频繁地成为有用的任务。可能会采取你们都给我的东西,并尝试在每个 div 之后制作两个按钮。追加和替换。谢谢大家的好信息。
    • 变量现在在那里。
    猜你喜欢
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 2012-02-16
    相关资源
    最近更新 更多