所以我的代码是这样的:-
for($i=0;$i<$length;$i++){
?>
<div id="scoreDiv">
<input type="text" name="myscore" placeholder="Enter your score" required>
<input type="text" name="oppoScore" placeholder="Your opponent score" required>
<input id="submitScore" type="submit" value="submit" >
</div>
<?php
}
?>
在
Jquery
中,我想检测单击了哪个按钮,并根据该按钮获取用户输入的输入字段。 最佳答案
你可以这样做(一个例子):-
$(document).ready(function(){
$('.submitScore').click(function(e){
e.preventDefault();
alert($(this).prev().attr('name'));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="scoreDiv">
<input type="text" name="myscore" placeholder="Enter your score" required>
<input type="text" name="oppoScore" placeholder="Your opponent score" required>
<input class="submitScore" type="submit" value="submit" >
</div>
<div id="scoreDiv">
<input type="text" name="myscore1" placeholder="Enter your score1" required>
<input type="text" name="oppoScore1" placeholder="Your opponent score1" required>
<input class="submitScore" type="submit" value="submit" >
</div>
注:-
class
被视为组选择器,而id
在Jquery
中被视为单个选择器。谢谢