如何在此输入框中捕获文本

时间:2019-02-11 14:52:00

标签: jquery

我没有ID来捕获文本,我只有div的ID

我尝试了

objeto=$('#TablaPaginadaNueva1_filter').parents('input'); 
userput=objeto.val();

但是我没有输入框的值

<div id="TablaPaginadaNueva1_filter" class="dataTables_filter">

    <label>Buscar:<input type="search" class="form-control form-control-sm" placeholder="" aria-controls="TablaPaginadaNueva1">
    </label>
</div>

2 个答案:

答案 0 :(得分:0)

var el = $('#TablaPaginadaNueva1_filter').find('input').first()

$(el).on('change', function(e){
  $('#output').html(e.target.value);
})

$('#setValBtn').on('click', function(){
  $(el).val('Hello');
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="TablaPaginadaNueva1_filter" class="dataTables_filter">
  <label>Buscar:<input type="search" class="form-control form-control-sm" placeholder="" aria-controls="TablaPaginadaNueva1">
    </label>
</div>


<h1 id="output"></h1>

<button id="setValBtn">Set value to Hello</button>

答案 1 :(得分:0)

只需一行代码即可获得它:

var objeto = $('#TablaPaginadaNueva1_filter label input').val();

然后清洁输入框:

$('#TablaPaginadaNueva1_filter label input').val('');