jQuery 表单插件 - 使用动态输入值循环提交

发布于 2024-11-06 20:59:14 字数 2010 浏览 0 评论 0原文

考虑以下设置:

  • 在页面加载时创建的表单

<输入 id="basevalue" name="basevalue" type="text" value="" />

现在,我设置了一个要循环的数组,该数组是根据 JSON 数据创建的。这一切都经过验证、检查并按预期工作。

我现在想做的是循环遍历数组以获得 Base64 编码的图像。我想用 PHP 来解码。为此,我想使用 post 方法来绕过 IE 浏览器的 URl 限制。

因此,在我的循环中,我使用 jQuery 表单插件将 #mybase64 表单提交到我的 PHP 文件。这一切几乎都有效。循环运行,调用 PHP 文件并更新目标元素中的图像。

问题 - 虽然我使用的是 for 循环,并且输入字段的值实际上得到了更新,但 jquery 表单在整个循环中不断提交相同的精确数据,尽管我更新了输入字段的值。

这就是我的做法,

for (var i = (JSpage - 1) * 12; i < JSresults; i++) {
    if (!JSON[i].firstname) { var firstName = ' '; } else { var firstName = JSON[i].firstname; }
    if (!JSON[i].lastname) { var lastName = ' '; } else { var lastName = JSON[i].lastname; }
    if (!JSON[i].photo_base64) {
        $('#ECbuttons').append('<li class="clickcontact" id="' + i + '"><img src="design_img/contact-no-image.jpg" /><div>' + firstName + '<br>' + lastName + '</div></li>');
    } else {
        //update input field
        $('#basevalue').val(JSON[i].photo_base64);

        //confirm that input field's value does change (check!)
        var mi = $('#basevalue').val();
        alert(mi);

        //Add new li element to be updated with new image
        $('#ECbuttons').append('<li class="clickcontact" id="' + i + '"><img style="height: 65px; width: 65px" src="" /><div>' + firstName + '<br>' + lastName + '</div></li>');
    }
    //dynamically set options for jQuery form submit
    var options = { 
            //target is handled correctly and keeps getting updated with the same image 
            target:     '#' + i, 
        url:        'system/showbase.php'
    }; 
    //submit the form on each run (check with a success handler, this gets called always
    $('#mybase64').ajaxSubmit(options); 
}

任何人都可以提供有关是否可以使用 ajax 表单请求从数组提交新数据的见解吗?如果可以,如何提交?

非常感谢您抽出时间

/Mikkel

Consider this setup:

  • Form created on page load

<form id="mybase64" method="POST">
<input id="basevalue" name="basevalue" type="text" value="" />
</form>

Now I set up an array to be looped which I have created from JSON data. This is all verified, checked and works as expected.

What I want to do now is loop through the array to get a base64 encoded image. This I want to decode using PHP. To do this, I want to use a post method to get around URl limit for IE browsers.

Therefore, in my loop I am submitting the #mybase64 form using the jQuery Form plugin to my PHP file. All this almost works. The loop runs, the PHP file is called and an image is updated in the target element.

The PROBLEM - Although I am using a for loop, and the input field's value does in fact get updated, jquery form keeps submitting the same exact data through the entire loop, despite the fact that I update the input field's value.

Heres is how I do that

for (var i = (JSpage - 1) * 12; i < JSresults; i++) {
    if (!JSON[i].firstname) { var firstName = ' '; } else { var firstName = JSON[i].firstname; }
    if (!JSON[i].lastname) { var lastName = ' '; } else { var lastName = JSON[i].lastname; }
    if (!JSON[i].photo_base64) {
        $('#ECbuttons').append('<li class="clickcontact" id="' + i + '"><img src="design_img/contact-no-image.jpg" /><div>' + firstName + '<br>' + lastName + '</div></li>');
    } else {
        //update input field
        $('#basevalue').val(JSON[i].photo_base64);

        //confirm that input field's value does change (check!)
        var mi = $('#basevalue').val();
        alert(mi);

        //Add new li element to be updated with new image
        $('#ECbuttons').append('<li class="clickcontact" id="' + i + '"><img style="height: 65px; width: 65px" src="" /><div>' + firstName + '<br>' + lastName + '</div></li>');
    }
    //dynamically set options for jQuery form submit
    var options = { 
            //target is handled correctly and keeps getting updated with the same image 
            target:     '#' + i, 
        url:        'system/showbase.php'
    }; 
    //submit the form on each run (check with a success handler, this gets called always
    $('#mybase64').ajaxSubmit(options); 
}

Can anyone offer any insight as to wether it is possible to submit new data from the array using the ajax form request, and if so, how?

Thank you very much for your time

/Mikkel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文