从后续输入中查找值并形成一个句子

时间:2015-07-08 05:09:14

标签: javascript jquery jquery-plugins jquery-events

我有 3 / 4 输入字段和它们之间的一些单词。

<div id="nl-form-0" > 
    Please remove stitch after 
      <input type="text" value="15" />
   days of discharge, i,e on 
     <input type="text" value="22/5/2016" />
     to improve your health in
     <input type="text" value="20" />
  days. 
</div>

我需要以句子形式输出:

  

&#34;请在放电15天后删除针脚,即2016年5月22日,以便在20天内改善您的健康状况。&#34;

4 个答案:

答案 0 :(得分:2)

编辑:

现在更新了JsFiddle,还会添加select中的选定值:

Jquery代码:

var html = $("#nl-form-0").clone();
    html.find('input,select').each(function(){
        var cell = $(this);
        $(this).replaceWith(cell.val());
     })
    var main =html.html();
    main = main.replace(/\s\s+/g, ' ');
    alert(main);

以下是答案:结帐Jsfiddle DEMO

Jquery代码:

var html = $("#nl-form-0").clone();
html.find('input').each(function(){
    var cell = $(this);
    $(this).replaceWith(cell.val());
 })
var main =html.html();
main = main.replace(/\s\s+/g, ' ');
alert(main);

答案 1 :(得分:1)

尝试

&#13;
&#13;
var text = $('#nl-form-0').contents().map(function() {
  if (this.nodeType == Node.TEXT_NODE && this.nodeValue.trim()) {
    return this.nodeValue.trim();
  } else if ($(this).is('input')) {
    return $(this).val()
  } else if ($(this).is('select')) {
    return $('option:selected', this).text()
  }
}).get().join(' ');

snippet.log(text)
&#13;
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="nl-form-0">
  Please remove stitch after
  <input type="text" value="15" />days of discharge, i,e on
  <input type="text" value="22/5/2016" />to improve your health in
  <input type="text" value="20" />days. also
  <select>
    <option value="1">One</option>
    <option value="2" selected>Two</option>
    <option value="3">Three</option>
  </select>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

这完全可以

var text = $('#nl-form-0').contents().map(function() {
  if (this.nodeType == Node.TEXT_NODE && this.nodeValue.trim()) {
    return this.nodeValue.trim();
  } else if ($(this).is('input')) {
    return $(this).val()
  }
}).get().join(' ');

snippet.log(text)
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="nl-form-0">
  Please remove stitch after
  <input type="text" value="15" />days of discharge, i,e on
  <input type="text" value="22/5/2016" />to improve your health in
  <input type="text" value="20" />days.
</div>

答案 3 :(得分:0)

看看这个。更干净(纯js,比jQuery优化)和简单的方法来做同样的事情。 [在Chrome上测试]

function myFunction() {
	var div = document.getElementById('test');
	var children = div.childNodes;
	var sentence = "";

	for (var i=0; i < children.length; i++) {
		var child = children[i];

		if (child.value) {
			sentence += child.value;
		}
		else {
			sentence += child.data;
		}
	}

	document.getElementById("output").innerHTML = sentence;
}
<div id="test" > 
    Please remove stitch after 
      <input type="text" value="15" />
   days of discharge, i,e on 
     <input type="text" value="22/5/2016" />
     to improve your health in
     <input type="text" value="20" />
  days. 
</div>

<span id="output"></span>
<input type="button" value="Click me" onclick="myFunction()"></input>