doune1000 2019-02-22 02:47
浏览 34

如何隔离javascript并将其绑定到特定表单

I have 3 forms on one page. The first two work fine. The 3rd is below the first 2. All have unique form and ID names.

The issue is the 3rd form depends on a javascript. If I run the 3rd by itself, it runs fine. But if I have the 3rd form on the page with its script and try to run either form above, the forms above break and somehow trigger the javascript that is meant to work only with the 3rd form.

If I take the script out, all three forms work no conflicts - except I lose my upload status bar for the 3rd form.

Here is the offending script:

<script>
$(function() {
$(document).ready(function(){
    var bar = $('#bar')
    var percent = $('#percent');
    var status = $('#status');

$('form').ajaxForm({
    beforeSend: function() {
     status.empty();
    var percentVal = '0%';
    bar.width(percentVal);
    percent.html(percentVal);
    },
uploadProgress: function(event, position, total, percentComplete) {
    var percentVal = percentComplete + '%';
    percent.html(percentVal);
    bar.width(percentVal);
    },
complete: function(xhr) {
    status.html(xhr.responseText);
    }
    });
});
});
</script>

If I change this:

$('form').ajaxForm({

to this, for example

 $('aform').ajaxForm({

it disables the script and allows all the other forms to work.

I have tried:

 $('this.form').ajaxForm({

and $('document.form').ajaxForm({

I tried using the name of the form:

 $('form.[3]').ajaxForm({

But that doesn't work

and I tried using the name of the form

 $('upload').ajaxForm({
 $('form.upload').ajaxForm({
 $('form[upload]').ajaxForm({

But nothing works. The code must use the term 'form' there or the progress bar breaks

There has to be some kind of way to link that script only to the form in question so the others won't go looking for it and try to execute it

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog