我想将数组的索引追加到其中select选项是数组的索引

时间:2016-04-27 13:42:49

标签: javascript php jquery

我想要一种情况,我可以将数组的索引作为select中的选项附加。以下代码失败。

<?php 
    $str4 = "select * from fee_names where status = '1' "; 
    $res4 = mysql_query($str4) or die(mysql_error()); 
    while ($r4 = mysql_fetch_assoc($res4)){ 
        $name = $r4['NAME']; 
        array_push($fee_nameArray,$name); 
    }
>?
<input type="text" name="fee_name" id="fee_name" value="<?php echo $fee_nameArray; ?>">    
$('#addClasses').click(function(){
    var arrayNAme = $('#fee_name').val();
    //alert(arrayNAme.length); return false;
    row++;
    $('#count').val(row);
    var feeName = "feeName"+row;
    var feeCat = "feeCat"+row;
    var freq = "freq"+row;
    var others = "others"+row;
    var mandate = "mandate"+row;
    var rowID = "rowID"+row;
    $('#table_mile35').prepend('<tr id="' + rowID + '"><td><select class="form-control" id="' + feeName + '" name="' + feeName + '" required><option value="" selected="selected">--Choose Class--</option>'
    for (i = 0; i < arrayNAme.length; i++) {
        '<option value="' + arrayNAme[i] + '">' + arrayNAme[i] + '</option>'
    }
    '</select></td><td><input type="text" class="form-control input-sm "  name="' + feeCat + '" id="' + feeCat + '" placeholder="school fees related, club related" required></td><td><input type="text" class="form-control input-sm "  name="' + freq + '" id="' + freq + '" placeholder="Yearly, Monthly" required></td><td><textarea type="text" class="form-control input-sm" name="' + others + '" id="' + others + '" placeholder="other relevant information" ></textarea></td><td><input type="checkbox"  name="' + mandate + '" id="' + mandate + '" value="1"></td></tr>');
 });

1 个答案:

答案 0 :(得分:0)

如果arrayNAme是一个数组,请像这样使用:

$('#addClasses').click(function(){
    var arrayNAme = $('#fee_name').val();
    arrayNAme = arrayNAme.split(',');
    row++;
    $('#count').val(row);
    var feeName = "feeName"+row;
    var feeCat = "feeCat"+row;
    var freq = "freq"+row;
    var others = "others"+row;
    var mandate = "mandate"+row;
    var rowID = "rowID"+row;
    var html = '<tr id="' + rowID + '"><td><select class="form-control" id="' + feeName + '" name="' + feeName + '" required><option value="" selected="selected">--Choose Class--</option>';
    for (i = 0; i < arrayNAme.length; i++) {
        html += '<option value="' + arrayNAme[i] + '">' + arrayNAme[i] + '</option>';
    }
    html += '</select></td><td><input type="text" class="form-control input-sm "  name="' + feeCat + '" id="' + feeCat + '" placeholder="school fees related, club related" required></td><td><input type="text" class="form-control input-sm "  name="' + freq + '" id="' + freq + '" placeholder="Yearly, Monthly" required></td><td><textarea type="text" class="form-control input-sm" name="' + others + '" id="' + others + '" placeholder="other relevant information" ></textarea></td><td><input type="checkbox"  name="' + mandate + '" id="' + mandate + '" value="1"></td></tr>';
    $('#table_mile35').prepend(html);
 });

我认为arrayNAme的值是以逗号分隔的(例如:学费,书费,注册费)