Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从列表中获取一个输入字段的数据值?_Php_Jquery - Fatal编程技术网

Php 如何从列表中获取一个输入字段的数据值?

Php 如何从列表中获取一个输入字段的数据值?,php,jquery,Php,Jquery,如何从列表中获取第一个输入字段的值 <button id="hello">get data</button><br><br> <table> <td><label for="voucher_<?php echo $counter; ?>">D/N Number <?php echo $counter; ?></label><br> <input

如何从列表中获取第一个输入字段的值

<button id="hello">get data</button><br><br>
   <table> <td><label for="voucher_<?php echo $counter; ?>">D/N Number <?php echo $counter; ?></label><br>
    <input type="text" name="voucher_<?php echo $counter; ?>" class="input-field exsmall-input" value="<?php echo $voucher; ?>" placeholder="D/N Number" id="voucher_<?php echo $counter; ?>" required>
    </td></table>

当我运行此javascript时,会得到未定义的值。

您可以按如下方式执行:-

$(document).ready(function(){
    $('#hello').click(function(){
          var name = $("div[id^='voucher']:visible:first").val();
        alert(name);
    });
});
或:-


出现错误的原因是使用了错误的选择器以输入元素为目标。输入元素没有id凭证。您需要输入ID以使现有代码正常工作

或者,您可以在此处从当前HTML使用以选择器开头的属性:

 $('#hello').click(function(){
      var name = $("input[name^='voucher_']").val();
      alert(name);
});
试试这个:-

$(document).ready(function() {
    $('#hello').click(function() {
        var name = $('table').find('input[type=text]').filter(':visible:first')
            .val();

    });
});
 $('#hello').click(function(){
      var name = $("input[name^='voucher_']").val();
      alert(name);
});
$(document).ready(function() {
    $('#hello').click(function() {
        var name = $('table').find('input[type=text]').filter(':visible:first')
            .val();

    });
});