douxiangshi6568 2015-07-10 14:21
浏览 81

php表单到javascript

I need to pass the form id to javascript, but always shows the same data.

<script>
$(document).ready(function() {
    $('input[name="submit"]').on('click', function(){
        var vauid = $( "input[name='uid']"     ).val(); alert("
 ID:  "+vauid);
    return false;
    }); 
});
</script>                                               
<? include "../includes/config.php";
$sql = "select * from table";
$reg = mysql_query($sql);
while($row = mysql_fetch_array($reg)){ ?>
<form action="" method="POST">
        <input type="text"   name="uid"     value="<?  echo $row['uid']?>" />
        <input type="submit" name="submit" value="Show"/>
</form> 
<? }?>
  • 写回答

1条回答 默认 最新

  • doufen5175 2015-07-10 14:27
    关注

    Part of the problem is that you're inserting numerous forms with elements having the same names - that is a bad idea as names are intended to be unique.

    Second, when you click you have to reference the currently clicked info:

    $(document).ready(function() {
        $('input[name="submit"]').on('click', function(e){
            e.preventDefault();
            var vauid = $(this).prev("input[name='uid']").val(); 
            console.log("ID:  "+vauid);
        }); 
    });
    

    Here is a working demo.

    $(this) will be the clicked button from which we look for the previous input's value. I'm also using preventDefault() to stop the button's default action, rather than returning false.

    In addition you should quit using alert() for troubleshooting., use console.log() instead.

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料