动态添加的文本字段具有第一个文本字段的默认值 [英] Dynamically added text field have default value of first text field

查看:154
本文介绍了动态添加的文本字段具有第一个文本字段的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,可以使用添加按钮添加文本字段,但其余字段会获得第一个字段的默认值。您可以在这里查看演示。只需在演示中的项目字段中键入Apple,您将获得69的价格。然后,当您点击添加项目按钮时,它会为所有生成的价格字段提供默认值69。我想显示新项目的空价格字段。



我的脚本:

添加动态行

 函数addRow(tableID){
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows [0] .cells.length; (var i = 0; i< colCount; i ++){
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows [0] .cells [i] .innerHTML;
//alert(newcell.childNodes);
switch(newcell.childNodes [0] .type){
casetext:
newcell.childNodes [0] .value =;
休息;
case复选框:
newcell.childNodes [0] .checked = false;
休息;
caseselect-one:
newcell.childNodes [0] .selectedIndex = 0;
休息;

$(function(){
var availableTags = [
Apple,
Samsung,
Blackberry,
Nokia,
];
$('input [name =item []]')。autocomplete({
source:availableTags
});
});
}
}

获取商品价格脚本

 函数getXMLHTTP(){//函数返回xml http对象
var xmlhttp = false;
尝试{
xmlhttp = new XMLHttpRequest();
}
catch(e){
try {
xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);

catch(e){
try {
xmlhttp = new ActiveXObject(Msxml2.XMLHTTP);
}
catch(e1){
xmlhttp = false;
}
}
}

return xmlhttp;
}
函数getproduct(element){

var strURL =getProduct.php?product =+ element.value;
var req = getXMLHTTP();

if(req){

req.onreadystatechange = function(){
if(req.readyState == 4){
// only如果OK
if(req.status == 200){
// last last here:
element.parentNode.parentNode.getElementsByClassName('productdiv')[0] .innerHTML = req.responseText;
} else {
alert(使用XMLHTTP时出现问题:\ n+ req.statusText);



req.open(GET,strURL,true);
req.send(null);
}
}

getProduct.php脚本:

 <?php 
$ product = $ _GET ['product'];
if($ product ==Apple){
$ value = 69;
} else {
$ value = 59;
}
?>
< input name =unit_price []value =<?php echo $ value;?> onkeyup =getValues()id =unit_priceplaceholder =Price/>


解决方案

mate,你问题在:newcell.innerHTML = table.rows [0] .cells [i] .innerHTML;

无论如何,您始终将第一行值赋予新添加的项目。

I have a script where I can add text fields with "Add Button" but the rest of fields get default value of first field. You can check the demo here. Just type Apple in item field in demo and you will get the price 69 for that. Then when you click "Add Item" button, it gives you default value of 69 for all generated price fields. I want to show the empty price field for new items.

My Script :

Add Dynamic Row

function addRow(tableID) {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);
            var colCount = table.rows[0].cells.length;
            for(var i=0; i<colCount; i++) {
                var newcell = row.insertCell(i);
                newcell.innerHTML = table.rows[0].cells[i].innerHTML;
                //alert(newcell.childNodes);
                switch(newcell.childNodes[0].type) {
                    case "text":
                            newcell.childNodes[0].value = "";
                            break;
                    case "checkbox":
                            newcell.childNodes[0].checked = false;
                            break;
                    case "select-one":
                            newcell.childNodes[0].selectedIndex = 0;
                            break;
                }
            $(function() {
              var availableTags = [
              "Apple",
              "Samsung",
              "Blackberry",
              "Nokia",
              ];
              $('input[name="item[]"]').autocomplete({
                  source: availableTags
              });
            });
            }
        }

Get Item Price Script

function getXMLHTTP() { //fuction to return the xml http object
        var xmlhttp=false;  
        try{
            xmlhttp=new XMLHttpRequest();
        }
        catch(e)    {       
            try{            
                xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){
                try{
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(e1){
                    xmlhttp=false;
                }
            }
        }

        return xmlhttp;
    }
      function getproduct(element) {      

        var strURL="getProduct.php?product="+element.value; 
        var req = getXMLHTTP();

        if (req) {

            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    // only if "OK"
                    if (req.status == 200) {   
                        // last changes here:
                        element.parentNode.parentNode.getElementsByClassName('productdiv')[0].innerHTML=req.responseText;                       
                    } else {
                        alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                    }
                }               
            }           
            req.open("GET", strURL, true);
            req.send(null);
        }       
    }

getProduct.php script :

<?php 
$product = $_GET['product'];
if($product == "Apple") {
    $value = 69;
} else {
    $value = 59;
}
?>
<input name="unit_price[]" value="<?php echo $value; ?>" onkeyup="getValues()" id="unit_price" placeholder="Price" />

解决方案

mate, you problem is at : newcell.innerHTML = table.rows[0].cells[i].innerHTML;

you always assign the first row value to the new added item no matter how

这篇关于动态添加的文本字段具有第一个文本字段的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆