【问题标题】:How to make label to appear instead of name?如何使标签出现而不是名称?
【发布时间】:2017-05-22 00:35:58
【问题描述】:

我的表单应该包含key=>value 对,其中key 应该是表单的labelvalue 应该是用户输入的详细信息,

我的问题是获取字段的name 而不是label

预计output:

         ENTER YOUR AGE:25

但我得到这样的输出:

           name-preview : 25;

如何实现预期的output

var result = [];

$('div label').each(function() {
  var $this = $(this);
  var label = $this.text();
  //console.log(label);

  value = $this.siblings().attr('value');
  //console.log(value);

  result.push({
    label: label,
    value: value
  });
});


console.log(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<form action="uploaddata.php" method="POST">

  <div class="form-group">
    <label for="text-1483332101835-preview" class="fb-text-label">enter your age:</label>

    <input type="text" class="form-control" name="age" value="25" id="mobNum">
  </div>

>

我该如何做这个elements:https://jsfiddle.net/ktgmtwd7/1/

【问题讨论】:

  • 不清楚您的预期输出是什么。你能详细说明一下吗?您可以使用name = $this.siblings().attr('name'); 获取名称

标签: javascript php jquery html node.js


【解决方案1】:

试试这个——当你改变输入值时,结果数组会自动更新:

var result = [];
var input;
$('div label').each(function() {
  var $this = $(this);
  var label = $this.text();
  input = $this.next();
  result.push({
    label: label,
    value: input.val()
  });
  console.log(result);
  $this.next().on('keyup', function() {
  	result = [];
  	result.push({
    	  label: label,
    	  value: input.val()
  	});
    console.clear()
    console.log(result);
  })
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="uploaddata.php" method="POST">
  <div class="form-group">
    <label for="text-1483332101835-preview" class="fb-text-label">enter your age:</label>
    <input type="text" class="form-control" name="age" value="25" id="mobNum">
  </div>
</form>

<h3></h3>

【讨论】:

    猜你喜欢
    • 2013-05-04
    • 2021-12-03
    • 2019-12-09
    • 1970-01-01
    • 2022-07-22
    • 1970-01-01
    • 2022-01-18
    • 2016-02-10
    • 1970-01-01
    相关资源
    最近更新 更多