简体   繁体   English

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

[英]jquery form submit with dynamic id not working

I can't submit the form with dynamic id.我无法提交带有动态 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() {在这一行中, $('#upload'+cvalue).submit(function() {

I can't get the cvalue.我无法获得 cvalue。 I can't identify what's wrong with this code.我无法确定这段代码有什么问题。 Anybody please help me.任何人请帮助我。

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?你真的确定你为 id 属性打印了一个正确的值吗?

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: 2nd:将上述行更改为以下内容:

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.注意:您定义了几个具有相同名称的 forms。 As far as I know, the attribute 'name' is the main attribute for forms, not the 'id' attribute.据我所知,“name”属性是 forms 的主要属性,而不是“id”属性。

I don't see where you assign anything besides "" or 0 to cvalue.除了“”或 0 给 cvalue 之外,我看不到你在哪里分配任何东西。 Also, those two assignments are outside of $(document).ready(function() { so there could be something weird going on with that code executing before your document has fully loaded. Move them inside of the document.ready call and see what happens.此外,这两个分配在 $(document).ready(function() { 之外,因此在您的文档完全加载之前执行该代码可能会发生一些奇怪的事情。将它们移动到 document.ready 调用中,看看会发生什么发生。

Try using尝试使用

var cvalue = "<?php echo $_GET['currentval']; ?>";

or或者

var cvalue = "<?php echo $_REQUEST['currentval']; ?>";

Also try what @reporter said about the form id: id='upload".$i."也试试@reporter 所说的关于表单 id 的内容:id='upload".$i." The form id should be the same as the one referenced here:表单 id 应与此处引用的相同:

$('#upload'+cvalue).submit(function() {

Example:例子:

in javascript:在 javascript 中:

$('#upload').submit(function() {

then form should be:那么形式应该是:

<form id="#upload" ...

You should definitely see something happen.你肯定会看到一些事情发生。 Try installing firebug and use console.log(variablehere) instead of alert(variablehere).尝试安装 firebug 并使用 console.log(variablehere) 而不是 alert(variablehere)。 It's more cleaner.它更清洁。

Hope that helps希望有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM