【问题标题】:How to clone a form and make the clone function exactly as the original?如何克隆表单并使克隆功能与原始表单完全相同?
【发布时间】:2012-05-03 14:39:28
【问题描述】:

我有以下表格:

    $(function() {
$("#message").keyup(function() {
var text_msg = $("#message").val();
    if(text_msg.length > 3 && text_msg.length <= 140)
    {
        $('#post').attr('disabled', '');
        $('#post').attr('value', 'Submit');
    }else{
        $('#post').attr('disabled', 'disabled');
        $('#post').attr('value', 'Wait...')
        if(text_msg.length > 3 && text_msg.length > 140){
            alert('Your message exceeded the number of characters which is 140. Delete a few letters.');
        }
    }

});

});

        $(function() {
$(".resp").click(function(){
    var idf = $(this).attr('id');   
    newform = $("form#form-msg").clone(true);
        $("#new-form-"+idf).append(newform);// These id's are generated dynamically
    });
 });

    <form method="post" name="form-msg" id="form-msg" action="javascript:func()"> 
        <textarea id="message" name="message"></textarea>
        <input type="submit" value="wait..." id="post" align="left" disabled="disabled" />
    </form>

<div id="new-form-1"></div>
<div class="resp" align="center" id="1">Reply</div>
<div id="new-form-2"></div>
<div class="resp" align="center" id="2">Reply</div>
<div id="new-form-3"></div>
<div class="resp" align="center" id="3">Reply</div>

当我克隆表单时,它不像原始表单那样工作,提交按钮没有激活,也没有处于提交表单的状态。一切都完美无缺,克隆形式的输入更少。如何使克隆函数与原函数相等?

【问题讨论】:

    标签: forms clone


    【解决方案1】:

    你需要删除属性而不是仅仅将它们空白,禁用属性的存在,即使它是空的也会导致按钮被禁用。对于 jQuery,这意味着 $(...).removeAttr('disabled') 而不是 $(...).attr('disabled', '')

    【讨论】:

    • 其他JQ代码有错误,修复这个错误但我意识到我无法获取字段中输入的值。
    猜你喜欢
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 2014-02-07
    • 1970-01-01
    相关资源
    最近更新 更多