gpt4 book ai didi

javascript - 如何将我的图书目录中的一系列图书分配给它们各自的作者?

转载 作者:数据小太阳 更新时间:2023-10-29 04:18:04 25 4
gpt4 key购买 nike

我想将添加的作者与可以由 jQuery 添加的其他作者分开。当我添加一位作者时,我希望能够将该作者链接到他的书,例如,在我按下添加作者按钮后,我得到了另一位作者的表格,但在我的代码中,所有作者都在一个数组中,所有的书都在其他。不与作者及其书籍分离。

我想通过邮寄方式发送:

author1: {book1, book2, book3}
author2: {book4, book5}
author3: {book6, book7, book8, book9}

这是我目前的代码:

<div class="container">
<form class="form-horizontal" role="form" action="next.php" method="post">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>


<div class="input_fields_wrap">
<button class="add_author">Add Author</button>

<div id="commonPart" class="commonPart">
<br>
<div><input type="text" name="myAuthorText[]" placeholder="Auth name"></div>

<button class="add_book">Add Author Books</button>
<div><input type="text" class="bookname" name="myBooksText[]" placeholder="Book name"></div>

</div>
</div>
<input name="Send" class="submit" value="Send" type="submit">

</form>
</div>
<script src="../js/jquery-2.1.4.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script src="../js/jqBootstrapValidation.js"></script>
<SCRIPT language="javascript">

$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var commonPart = $("#commonPart");
var add_author = $(".add_author"); //Add button ID
var add_subButton = $(".add_book"); //Add sub button ID

var x = 1; //initlal text box count
$(add_author).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
var htmlToAdd = commonPart.clone().attr("id","commonPart_"+x);
htmlToAdd.find(".addedDiv").remove();
$(wrapper).append(htmlToAdd); //add input box
x++; //text box increment
}
});

$(document).on("click",".add_book",function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(this).closest(".commonPart").append('<div class="addedDiv"><input type="text" class="bookname" name="myBooksText[]" placeholder="Book name"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
}
});

$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});

//http://www.sanwebe.com/2013/03/addremove-input-fields-dynamically-with-jquery
</SCRIPT>

JSFiddle 链接:

JSFiddle I

JSFiddle II (collaboration)

最佳答案

这是我发现我本可以做到的!

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<style type="text/css">
<!--
#main {
max-width: 800px;
margin: 0 auto;
}
-->
</style>
</head>
<body>
<div id="main">
<h1>Add or Remove text boxes with jQuery</h1>
<button onclick="addAuthor()" >Add Author</button><br><br>
<div id="addAuth"></div>
<br><br>
<button onclick="submit()" >Submit</button>
</div>

<div id="result" ></div>
</div>

<script type="text/javascript">
var authors = 0;

function addAuthor(){
authors++;
var str = '<br/>'
+'<div id="auth'+authors+'"><input type="text" name="author" id="author'+authors+'" placeholder="Author Name:"/>'
+'<br/>'
+'<button onclick="addMore(\'auth'+authors+'\')" >Add Book</button>'
+'</div>';
$("#addAuth").append(str);
}

var count=0;
function addMore(id){
count++;
var str = '<div id="bookDiv'+count+'">'
+'<input class="'+id+'" type="text" name="book'+id+'" placeholder="Book Name"/>'
// +'<span onclick="addMore(\''+id+'\')" style="font-size: 20px; background-color: green; cursor:pointer;">+</span>'
+'<span onclick="removeDiv(\'bookDiv'+count+'\')" style="font-size: 20px; background-color: red; cursor:pointer; margin-left:1%;">Remove</span>'
// +'<a href="#" class="remove_field">Remove</a></div>'
+'</div>';
$("#"+id).append(str);
}

function removeDiv(id){
//var val = confirm("Are you sure ..?");
//if(val){
$("#"+id).slideUp(function(){
$("#"+id).remove();
});
//}
}

function submit(){
var arr = [];
for(i=1; i<=authors; i++){
var obj = {};
obj.name = $("#author"+i).val();
obj.books = [];
$(".auth"+i).each(function(){
var data = $(this).val();
obj.books.push(data);
});

arr.push(obj);
}

$("#result").html(JSON.stringify(arr));
}
</script>
</body>
</html>

关于javascript - 如何将我的图书目录中的一系列图书分配给它们各自的作者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34286629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com