0

我在页面加载时有一组固定的输入字段。我有显示值的复选框,当有人选中复选框时,值将添加到输入字段中。如果填写了所有输入字段,则会创建一个新字段。我的问题是,复选框值已正确插入现有输入字段,如果值超过,则会创建一个新输入字段,但在创建输入字段时不会立即插入值。只有在下一次单击时才会插入值新创建的输入字段。这是代码

<script>
    function fillin(entire,name,id,key) {
        if (entire.checked == true) {
            var split_info = new Array();
            split_info = name.split(":"); 
            var div = $("#Inputfields"+id);
            var till = (div.children("input").length)/4;
            var current_count = 0;
            for (var j=0;j<till;j++) {
                if (document.getElementById("insertname_"+j+"_"+id).value == "" && document.getElementById("insertnumber_"+j+"_"+id).value == "") {
                    document.getElementById("insertname_"+j+"_"+id).value = split_info[0];
                    document.getElementById("insertnumber_"+j+"_"+id).value = split_info[1];
                    break;
                } else
                    current_count = current_count+1;
                if (current_count == till) {
                    var x= addnew(id);
                    x =x+1;
                    $("#Inputfields"+id).find("#insertname_"+x+"_"+id).value = split_info[0];
                    alert($("#Inputfields"+id).find("#insertname_"+x+"_"+id).value);
                    document.getElementById("insertname_"+x+"_"+id).text = split_info[0];
                    //alert(document.getElementById("insertname_"+x+"_"+id).value);
                    //document.getElementById("insertnumber_"+x+"_"+id).value = split_info[1];
                }
            }
        } else {
        }
    }
</script>

<script>
function addnew(n) {
    //var id = $(this).attr("id");
    var div = $("#Inputfields"+n);
    var howManyInputs = (div.children("input").length)/4;
    alert(howManyInputs);
    var val = $("div").data("addedCount");

    var a = '<input type="search" id="insertinstitute_'+(howManyInputs)+'_'+n+'" placeholder="Institute" class="span3">';
    var b =  '<input type="search" id="insertname_'+(howManyInputs)+'_'+n+'" placeholder="name" class="span3">';
    var c =  '<input type="search" name="" id="insertnumber_'+(howManyInputs)+'_'+n+'" placeholder="number" class="span3">';
    var d =  '<input type="search" name="" id="insertarea_'+(howManyInputs)+'_'+n+'" placeholder="area" class="span3">';
    var fin = a+b+d+c;
    $(fin).appendTo(div);
    div.data("addedCount", div.data("addedCount") + 1);
    return howManyInputs;
}
</script>

更新:谢谢大家。我能够找到错误。罪魁祸首是 x =x+1;。应该是x

4

2 回答 2

5

问题大概出在这里:

document.getElementById("insertname_"+x+"_"+id).text

元素中没有text属性。有textContent(不在 IE8- 中)、innerText(在 IE 中)和innerHTML. 不过,jQuery 中有text方法。所以你可以这样做:

document.getElementById("insertname_"+x+"_"+id).innerHTML = ...

或者

$("#insertname_"+x+"_"+id).text(...);

此外,这些行:

$("#Inputfields"+id).find("#insertname_"+x+"_"+id).value = split_info[0];
alert($("#Inputfields"+id).find("#insertname_"+x+"_"+id).value);

.value应该替换为.val(),因为这些是 jQuery 对象。

于 2013-04-19T13:00:16.690 回答
1

由于很多原因,我已经修改了很多代码。比较两者。

function fillin(entire, name, id, key) {
    if (entire.checked) {
        var split_info = [];
        split_info = name.split(":");
        var div = $("#Inputfields" + id);
        var till = (div.children("input").length) / 4;
        var current_count = 0;
        var j = 0;
        for (j = 0; j < till; j++) {
            var myj = j + "_" + id;
            if ($("#insertname_" + myj).val() === "" && $("#insertnumber_" + myj).val() === "") {
                $("#insertname_" + myj).val(split_info[0]);
                $("#insertnumber_" + myj).val(split_info[1]);
                break;
            } else {
                current_count = current_count + 1;
            }
            if (current_count === till) {
                var x = addnew(id) + 1;
                div.find("#insertname_" + x + "_" + id).val(split_info[0]);
                alert(div.find("#insertname_" + x + "_" + id).val());
                $("#insertname_" + x + "_" + id).val(split_info[0]);
            }
        }
    }
}

function addnew(n) {
    var div = $("#Inputfields" + n);
    var howManyInputs = (div.children("input").length) / 4;
    alert(howManyInputs);
    var myi = (howManyInputs) + '_' + n + '"';
    var val = div.data("addedCount");

    var a = '<input type="search" id="insertinstitute_' + myi + ' placeholder="Institute" class="span3">';
    var b = '<input type="search" id="insertname_' + myi + '  placeholder="name" class="span3">';
    var c = '<input type="search" name="" id="insertnumber_' + myi + ' placeholder="number" class="span3">';
    var d = '<input type="search" name="" id="insertarea_' + myi + '  placeholder="area" class="span3">';
    var fin = a + b + d + c;
    $(fin).appendTo(div);
    div.data("addedCount", val + 1);
    return howManyInputs;
}
于 2013-04-19T13:52:43.473 回答