好的,这是我现在的新 javascript 代码:
var counter = <? echo $count-1 ?>;
var limit = 24;
function addInput(divName){
if (counter >= limit) {
alert("You have reached the limit of adding " + counter + " inputs");
}
else {
var newdiv = document.createElement('div');
var dropDown;
var fromPHP=<? echo $s_to_json ?>;
for (i=0; i<fromPHP.length; i++){
yourValue = fromPHP[i];
dropDown = dropDown + "<option value='" + yourValue + "'>" + yourValue + "</option>";
}
//document.write(dropDown);
var containerType = "<?echo $matRow['container']?>";
var viz;
if (containerType == "Box/Bag"){
viz = "visible";
}else{
viz = "hidden";
}
newdiv.innerHTML = "Weight " + (counter + 1) + ": <input type='text' name='myWeights[]' class='putty' maxlength='6' /> Material " + (counter + 1) + ": <input type='text' name='myMaterials[]' class='plato' maxlength='17' value='<? echo (isset($matRow['material']) ? $matRow['material'] : '')?>' /> Container " + (counter + 1) + ": <select onchange=\"switchMain(this.nextSibling.nextSibling.id);\" name='myContainers[]' id='myContainers[]'><option value='<? echo (isset($matRow['container']) ? $matRow['container'] : '') ?>' selected><? echo (isset($matRow['container']) ? $matRow['container'] : '') ?></option>' + dropDown + '" + dropDown + "</select><div id='boxhide[]' style='visibility:" + viz + ";display:inline;'> Quantity " + (counter + 1) + ": <input type='text' name='boxnum" + (counter + 1) +"' class='boxputty' maxlength='2' /></div>";
document.getElementById(divName).lastChild.parentNode.appendChild(newdiv);
counter++;
}
}
function getStyle(divName){
var temp = document.getElementById(divName).style.visibility;
return temp;
}
function switchMain(divName){
//var e = document.getElementById("myContainers[]");
//var strUser = e.options[e.selectedIndex].value;
//console.debug(strUser);
//console.debug(divName);
if (strUser == "Box/Bag"){
var current = getStyle(divName);
//console.debug(current);
document.getElementById(divName).style.visibility = "visible";
}else{
document.getElementById(divName).style.visibility = "hidden";
}
}
function removeInput(divName){
if (counter<=1){
alert("You cannot remove any more weights from this job.");
}
else {
var olddiv = document.getElementById(divName);
var lastChild = olddiv.lastChild;
if (lastChild && lastChild.parentNode && lastChild.parentNode.removeChild){
lastChild.parentNode.removeChild(lastChild);
counter--;
}
}
}
这是调用它的脚本部分:
Weight <? echo $count ?>: <input type="text" class="putty" name="myWeights[]" maxlength="6" value="<? echo $read ?>" autofocus /> Material <? echo $count ?>: <input type="text" class="plato" name="myMaterials[]" maxlength="17" value="<? echo (isset($matRow["material"]) ? $matRow["material"] : "") ?>" />
Container <? echo $count ?>: <select onchange="switchMain(this.nextSibling.nextSibling.id);" name="myContainers[]" id="myContainers[]">
<option value="<? echo (isset($matRow["container"]) ? $matRow["container"] : "") ?>" selected><? echo (isset($matRow["container"]) ? $matRow["container"] : ""); ($matRow['container'] == 'Box/Bag' ? $vishid = 'visible' : $vishid = 'hidden') ?></option>
<?
<div id='boxhide[]' style="visibility:<? echo $vishid ?>;display:inline;">
Quantity <? echo $count ?>: <input type="text" name="boxnum[]" class="boxputty" maxlength="2" />
</div>
<input type="button" alt='Add Weight' title='Add Weight' value="+" onClick="addInput('dynamicInput');">
<input type="button" alt='Remove Weight' title='Remove Weight' value="-" onClick="removeInput('dynamicInput');">
我剪掉了所有不必要的部分。
截至目前,当用户单击以添加一行时,它会将其放置在错误的位置,但我确信我可以像以前一样对其进行排序。更大的问题是,通过单击“+”按钮添加的任何行在更改由 switchMain() 检查的下拉菜单时都会出错。错误显示“TypeError:this.nextSibling.nextSibling 为空”。
任何建议都会很棒!谢谢!