dongpin1059 2011-07-08 06:34
浏览 44
已采纳

jquery表单提交,动态ID不起作用

I can't submit the form with dynamic id. Below is my code.

<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>
<script type="text/javascript">
var fval;    
var cvalue = "<?php echo $_POST['currentval']; ?>";
if(!(cvalue)) cvalue=0;
$(document).ready(function() {
$('#upload'+cvalue).submit(function() {
    var options = {
        target: '#message', 
        url:'process.php?sval='+cvalue, 
        success:  function() {
        alert("success");
        $('#uploader').html('');

        }
    };
$(this).ajaxSubmit(options);
return false;
});
});
</script>
<div id="message"></div>
<?php
for($i=0;$i<5;$i++) {
    echo "<form action='#' method='post'  name='upload' id='upload$i' enctype='multipart/form-data'>";   
    echo "<input type='hidden' name='svalhid'  id='svalhid' value='$i'>";
    echo "<input type='file' id='fl$i' name='filename".$i."up'>";
    echo "<input type='hidden' name='currentval' id='currentval' value='$i'>";
    echo "<input type='submit' name='uploads$i' value='Ok'><br>";
    echo '</form>';
}
?>

In this line, $('#upload'+cvalue).submit(function() {

I can't get the cvalue. I can't identify what's wrong with this code. Anybody please help me.

  • 写回答

3条回答 默认 最新

  • drgdn82648 2011-07-08 06:56
    关注

    Check follow line

    echo "<form action='#' method='post'  name='upload' id='upload$i' enctype='multipart/form-data'>";
    

    Are you really sure that you printed out a correct value for the id attribute?

    I think it exists two better ways:

    1st (the best way in my eyes):

    <?php
    for($i=0;$i<5;$i++) {
    ?>
        <form action='#' method='post'  name='upload' id='upload<?php echo $i ?>' enctype='multipart/form-data'>";   
        <input type='hidden' name='svalhid'  id='svalhid' value='<?php echo $i ?>'>"
        <input type='file' id='fl<?php echo $i ?>' name='filename<?php echo $i ?>up'>";
        <input type='hidden' name='currentval' id='currentval' value='<?php echo$i ?>'>"
        <input type='submit' name='uploads<?php echo $i ?>' value='Ok'><br>"
        </form>'
    <?php
    }
    ?>
    

    2nd: Change the mentioned line to following:

    echo "<form action='#' method='post'  name='upload".$i."' id='upload".$i."' enctype='multipart/form-data'>";
    

    Note: You defined a couple of forms with the same name. As far as I know, the attribute 'name' is the main attribute for forms, not the 'id' attribute.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示