dounai9294 2014-04-12 15:04
浏览 11

JQuery采用了错误的值

An easy question for you I think :-)

<form id="upload" method="post" name="form">  
<input id="id<?php echo $a; ?>" class="id" name="id" type="text" value="<?php echo $id_submit; ?>" >
<input style="margin-top:-5px;width:29px;" class="submit" type="image" src="save.jpg">
</form>

The source code looks e.g. like this:

Form 1:

<form id="upload" method="post" name="form">  
<input id="id1" class="id" name="id" type="text" value="12345" >
<input style="margin-top:-5px;width:29px;" class="submit" type="image" src="save.jpg">
</form>

Form 2:

<form id="upload" method="post" name="form">  
<input id="id2" class="id" name="id" type="text" value="65432" >
<input style="margin-top:-5px;width:29px;" class="submit" type="image" src="save.jpg">
</form>

After click on save.jpg it calls my jquery-function:

$(document).ready(function() {                              
                           $( "#upload" ).on("submit", function(e) {

                               e.preventDefault();

                                               var id = $('.id').val();
                                               …..

The problem is that when I click on save.jpg in the form 2, then it takes the value 12345 instead of 65432.

How can I fix that?

  • 写回答

3条回答 默认 最新

  • douqiao7958 2014-04-12 15:06
    关注

    the form is needs to be unique

    <form id="upload" method="post" name="form">

    thats why it takes the first element found in the dom structure with the target id, change the id of the second form .

    Also when you do

    var id = $('.id').val();

    there a more than one element with the given class. you can try this instead

    var id = $(this).find('.id').val();

    Happy Coding :)

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?