0

嗨,我正在尝试创建某种家谱。我有一个下拉框,当我创建一个新家庭并提示用户添加所选家庭的孩子时,它会动态添加选项。

我正在尝试将孩子附加到所选家庭的表格之后,但我不知道动态生成它们时会生成什么样的 ID

代码如下

<!DOCTYPE html>

<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function () {
//To Create Familes 
$('#submit').on('click', function() {
        var table = $('<table></table>').addClass('table');
    var family = document.getElementById('famname').value;

    $('#family input[type = text],input[type = text],input[type =    text],input[type = text]').each( function() {
    str =  '<td>' + $(this).val() + '</td>';
    $(this).val('');
    table.append( str );
    }); 
    $('#container').append(table);
    $('#select').append($('<option />', { text: family } ) );
});

//To Create child to right family 
$('#submit2').on('click', function() {

var child = prompt("Enter the name of the child you wanna put to the selected family ");
something.after(child);
});
});

</script>

</head>

<body>

<form id ="family" method = "post" target="_parent">
   Enter Family Name <input type = "text" name = "famname" id = "famname" > <br>
   Enter Address <input type = "text" name = "address"> <br>
   Enter Father's name <input type = "text" name = "dad"> <br>
   Enter Mother's name<input type = "text" name = "mom"> <br>
   <input id="submit" type="button" value="Submit" name="submit">
</form>

<p>Select Which family you want to add a child to</p>
<form id = "child">
   <select id ="select"></select>
   <input id="submit2" type="button" value="Submit" name="submit">
</form>

<div id  = "container">
</div>

</body>
</html>

关于如何在选定的家庭之后追加的任何想法?还是有更好的做法

4

3 回答 3

0

这不是很清楚你到底想要做什么,但这是一个更整洁的版本,它在家庭行之后将孩子添加为新行:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="mystyle.css">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.js"></script>
    <script>
      $(function() {
        var cur_id = 0;

        //To Create Familes 
        $('#submit').on("click", function(ev) {
          var id = "family" + cur_id++,
            $row = $('<tr id="' + id + '"></tr>');

          $('#select').append('<option value="' + id + '">' + $("#famname").val() + '</option>');

          $('#family :text')
            .each(function() {
              $row.append('<td>' + $(this).val() + '</td>');
            })
            .val("");

          $('#container table').append($row);
        });

        //To Create child to right family 
        $('#submit2').click(function(ev) {
          var child = prompt("Enter the name of the child you wanna put to the selected family "),
              id = $("#select").val();

          $("#" + id)
            .after("<tr><td>" + child +  "</td></tr>");
        });
      });
    </script>
  </head>
  <body>
    <form id ="family" method = "post" target="_parent">
      <label>Enter Family Name <input type="text" name="famname" id="famname"></label><br>
      <label>Enter Address <input type="text" name="address"></label><br>
      <label>Enter Father's name <input type = "text" name="dad"></label><br>
      <label>Enter Mother's name <input type="text" name="mom"></label><br>
      <input id="submit" type="button" value="Submit" name="submit">
    </form>

    <p>Select Which family you want to add a child to</p>
    <form id="child">
      <select id="select"></select>
      <input id="submit2" type="button" value="Submit" name="submit">
    </form>

    <div id="container">
      <table class="table"></table>
    </div>
  </body>
</html>
于 2013-10-10T01:55:52.307 回答
0

最好的方法是实际添加元素而不是 HTML,例如:

var test = document.createElement('table');
test.setAttribute('id', 'test_id');
document.body.appendChild(test);
console.log(document.getElementById('test_id'));

要引用它,请务必使用 setAttribute javascript 调用或 jquery 中的 .attr() 或 .prop() 方法添加 id 和/或类。

于 2013-10-10T01:02:08.123 回答
0
    <!DOCTYPE html>

<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function () {

//To Create Familes 
$('#submit').on('click',function(){

    var table = $('<table></table>').addClass('table');
    var family = document.getElementById('famname').value;

    $('#family input[type = text],input[type = text],input[type =    text],input[type = text]').each(function() {

        str =  '<td>' + $(this).val() + '</td>';

        $(this).val('');

        table.append(str);
        table.addClass(family);


    });





    $('#container').append(table);

    $('#select').append($('<option />', {text: family, value:family}));



});

//To Create child to right family 
var fam, child, str;

$(document).on('change', '#select', function(){

    $('#submit2').show();
    $('#child_name').show();

    fam = $(this).children('#select :selected').val();

});


$(document).on('click', '#submit2', function(){
    child = $('#child_name').val();

    $('#submit2').hide();
    $('#child_name').hide();

    str =  '<td>' + child + '</td>'
    $('.'+fam).append(str);
});



});

</script>

</head>

<body>



<form id ="family" method = "post" target="_parent">
Enter Family Name <input type = "text" name = "famname" id = "famname" > <br>
Enter Address <input type = "text" name = "address"> <br>
 Enter Father's name <input type = "text" name = "dad"> <br>
Enter Mother's name<input type = "text" name = "mom"> <br>
<input id="submit" type="button" value="Submit" name="submit">
</form>

<p>Select Which family you want to add a child to</p>

<select id ="select">
</select>
<input type="text" id="child_name" style="display:none;">
<input id="submit2" type="submit" value="button" name="submit" style="display:none;">


<div id  = "container">

</div>

</body>
</html>

这应该可以,我已经测试过了......希望这会有所帮助

于 2013-10-10T03:04:43.200 回答