简体   繁体   English

.val()仅返回PHP数组中的第一行

[英].val() Only Returns 1st row in PHP array

I have a PHP form which is used for updating records which have come from an array, I'm currently testing a button to show the current Name in the record that appears but my issue is that the 1st row record always comes through even when I click on a different row in my array. 我有一个PHP表单,用于更新来自数组的记录,我目前正在测试一个按钮,以在显示的记录中显示当前名称,但我的问题是即使我遇到第一个行记录也总是通过单击数组中的另一行。

I have a simple form: 我有一个简单的表格:

<form id="update_form">
<div class="row">
<div class="col">
<label>Name</label>  
<input type="text" name="fnameupdate" id="fnameupdate" class="form-control fnameupdate" value="<?php echo $row['Firstname'] ?>" /> 
</div>
</div>
</form>
<button name="updaterecord" id="updaterecord" class="btn btn-success omhformupdate floatleft"><i class="material-icons floatleft">add_circle</i><div>Update Appointment</div></button>

When the button is clicked this is the event thats being fired: 单击该按钮时,将触发该事件:

    <script>  
 $(document).ready(function(){  
  $('.omhformupdate').click(function(){  

       var nameupdate = $('#fnameupdate').val();  
              window.alert(nameupdate);

        });  
    });  
</script>

The alert does fire however only the first row is coming back all the time even though another row has been selected, What is causing this? 警报确实会触发,但是即使选择了另一行,也只会一直返回第一行,这是什么原因造成的?

First of all, IDs must be unique, and an ID selector will just select the first element on the page with that ID. 首先,ID必须是唯一的,并且ID选择器将只选择页面上具有该ID的第一个元素。 You should use a class selector. 您应该使用类选择器。

Second, if you want the value of the input right before the button, you need to use a DOM traversal methods to find it, not just any element with the class. 其次,如果您希望输入的值恰好位于按钮之前,则需要使用DOM遍历方法来查找它,而不仅仅是类中的任何元素。

 $(document).ready(function() { $('.omhformupdate').click(function() { var nameupdate = $(this).prev().find('.fnameupdate').val(); window.alert(nameupdate); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="update_form"> <div class="row"> <div class="col"> <label>Name</label> <input type="text" name="fnameupdate" id="fnameupdate" class="form-control fnameupdate" value="" /> </div> </div> </form> <button name="updaterecord" id="updaterecord" class="btn btn-success omhformupdate floatleft"><i class="material-icons floatleft">add_circle</i><div>Update Appointment</div></button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM