简体   繁体   English

jQuery添加表单,可在JSFiddle上工作,但在生产中无法使用

[英]Jquery adding forms, works on JSFiddle but in production does not

Hi guys this might be a really stupid error but im using jquery to add a formset to a page it also does other things such as updating the number of forms but that does not seem to be a issue. 嗨,大家好,这可能是一个非常愚蠢的错误,但是即时通讯使用jquery向页面添加了一个表单集,它还执行其他操作,例如更新表单数,但这似乎不是问题。

http://jsfiddle.net/b5Y8f/ http://jsfiddle.net/b5Y8f/

$(document).ready(function () {
function updateElementIndex(el, prefix, ndx) {
    var id_regex = new RegExp('(' + prefix + '_set-\\d+-)');
    var replacement = prefix + '_set-' + ndx + '-';
    if ($(el).attr("for")) $(el).attr("for", $(el).attr("for").replace(id_regex, replacement));
    if (el.id) el.id = el.id.replace(id_regex, replacement);
    if (el.name) el.name = el.name.replace(id_regex, replacement);
}

function changeDeleteForms(el, prefix, formid) {
    var idstring = 'id_' + prefix + '_set-' + formid + '-DELETE';
    //$('<input>').attr({type: 'hidden', id: 'id_' + idstring, name: idstring}).appendTo('.command-delete');
    $('#' + idstring).prop('checked', true);
}

function deleteForm(btn, prefix) {
    var formCount = parseInt($('#id_' + prefix + '_set-TOTAL_FORMS').val());
    if (formCount > 1) {
        // Delete the item/form
        $(btn).parents('.command').hide();
        $(btn).parents('.command').attr('class', 'command-delete');
        var dc = $(".command-delete");
        $(dc).children().children().children().each(function () {
            var formid = this.id.match(/\d+/g);
            changeDeleteForms(this, prefix, formid);
            //$(this).val("");
        });
        var forms = $('.command'); // Get all the forms
        var formsdelete = $('.command-delete'); // Get all the forms 
        var fl = parseInt(forms.length);
        var fdl = parseInt(formsdelete.length);
        var finalcount = fl + fdl
        // Update the total number of forms (1 less than before)
        //$('#id_' + prefix + '_set-TOTAL_FORMS').val(forms.length);
        var i = 0;
    } // End if
    else {
        alert("Please enter atleast 1 command for this item.");
    }
    return false;
}

function addForm(btn, prefix) {
    var formCount = parseInt($('#id_' + prefix + '_set-TOTAL_FORMS').val());
    var maxCount = parseInt($('#id_' + prefix + '_set-MAX_NUM_FORMS').val());
    var forms = parseInt($('.command-delete').length); // Get all the forms 
    var newcount = formCount + forms;
    // You can only submit a maximum of 10 todo items
    if (newcount < maxCount) {
        // Clone a form (without event handlers) from the first form
        var row = $(".command:first").clone(false).get(0);
        // Insert it after the last form
        $(row).removeAttr('id').hide().insertAfter(".command:last").slideDown(300);

        // Remove the bits we don't want in the new row/form
        // e.g. error messages
        $(".errorlist", row).remove();
        $(row).children().removeClass("error");

        // Relabel or rename all the relevant bits
        $(row).children().children().children().children().each(function () {
            updateElementIndex(this, prefix, newcount);
            $(this).val("");
        });

        // Add an event handler for the delete item/form link 
        $(row).find(".delete").click(function () {
            return deleteForm(this, prefix);
        });
        // Update the total form count
        $("#id_" + prefix + "_set-TOTAL_FORMS").val(newcount + 1);
    } // End if
    else {
        alert("Sorry, you can only enter a maximum of 1000 items.");
    }
    return false;
}
// Register the click event handlers
$("#add").click(function () {
    return addForm(this, "itemcommands");
});

$(".delete").click(function () {
    return deleteForm(this, "itemcommands");
});

$('.command input:checkbox').hide();

});

If you go to the link above you can see the code works perfectly fine it update the form count and add the new form with the new number in the id and everything however in production when you click the add command button for the first 3 times it does not show however the code has been enter into the page and the form is technically there but not shown. 如果您转到上面的链接,您会看到代码可以正常工作,它可以更新表单计数,并在id和其他所有内容中添加带有新数字的新表单,而在您单击添加命令按钮的前3次时,它将处于生产状态不会显示,但是代码已输入到页面中,并且该表单在技术上在那里但未显示。

on the fourth time you press the button it works and the row has been added after the last ('.command') in the element. 在第四次按下按钮时,该按钮将起作用,并且该行已添加到元素中的最后一个('.command')之后。

What could be causing it to work on JSFiddle but not on production? 是什么导致它无法在JSFiddle上运行但不能在生产上运行?

-------------------UPDATE-------------------------- ------------------- UPDATE --------------------------

It seems if i remove the overflow hidden from the 3 that dont show when you press the button the first 3 times it will show them in the correct place. 看来,如果我从前3次按下按钮时删除了3个未显示的隐藏的溢出,它将在正确的位置显示它们。

Why would overflow no be removed from the first 3 form rows but the rest after fine? 为什么没有从前3个表单行中删除溢出,而是在罚款之后将其余部分除去?

----------------------UPDATE-------------------------- Think i have found the issue and its nothing to do with the JQUERY at all it seems to be bootstraps responsive layout hiding the forms i think if i add them specifically to their own rows i can fix this. ----------------------更新--------------------------思考我发现了这个问题,它与JQUERY毫无关系,似乎是引导程序响应式布局,隐藏了表单,我认为如果我将它们专门添加到自己的行中,则可以解决此问题。

Thanks for the help though guys. 谢谢你们的帮助。

I don't see a src="*jQuery source*" in your file. 我在您的文件中看不到src="*jQuery source*" Since JSFiddle already adds the source to the file, you may have forgotten to put it in. 由于JSFiddle已经将源添加到文件中,因此您可能忘记了将其放入。

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

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