JQuery中的警报数据,在PHP for循环中。

JQuery中的警报数据,在PHP for循环中。,php,jquery,Php,Jquery,我有一个PHP循环中的代码 <?php for($i = 1; $i++; $i < 10) { ?> <div class="test"> <textarea name="test-textarea" for="<?php echo $i ?>"></textarea> <button value="<?php echo $i ?>">Save</button> </div> &l

我有一个PHP循环中的代码

<?php for($i = 1; $i++; $i < 10) { ?>
<div class="test">
<textarea name="test-textarea" for="<?php echo $i ?>"></textarea>
<button value="<?php echo $i ?>">Save</button>
</div>
<?php } ?>


您可以在属性选择器中包含按钮的值,以查找链接的文本区域

$('button').click(function(){
    alert( $('textarea[for="' + $(this).attr('value') + '"]').val() );
});
最好对自定义数据使用
data-*
属性。

试试这个

$(document).ready(function(){
    $(".test button").click(function(){
        alert($(this).parent().find('textarea').val());
    });
});
请参见试试这个

<?php for($i = 1; $i < 10; $i++) { ?>
<div class="test">
<textarea id="<?php echo $i ?>" name="test-textarea" for="<?php echo $i ?>"></textarea>
<button onclick="javacript:alertvalue(<?php echo $i; ?>);" value="<?php echo $i ?>">Save</button>
</div>
<?php } ?>
<script type="text/javascript">
function alertvalue(var tid){
var txt=document.getElementById(tid).value();
alert(txt);
}
</script>