javascript - 在按钮 onclick 中创建动态文本框并将数据保存到数据库

标签 javascript php jquery html

我正在尝试使用 php 和 jquery 创建动态文本框。我想提交一篇论文进行演示,每个演示文稿都有多个作者,每个作者都有多个隶属关系。我尝试为作者及其所属机构创建动态文本框。我可以动态创建最多 10 位作者,但仅限第一作者的隶属关系。任何人请帮我纠正这个代码。谢谢

<html>
<head>
<title>author</title>
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
 
<style type="text/css">
	div{
		padding:8px;
	}
</style>
 
</head>
 
<body>
 
<h1>Author</h1>
 
<script type="text/javascript">
 
$(document).ready(function(){
 
    var counter = 2;
 
    $("#addAuthor").click(function () {
 
	if(counter>10){
            alert("Only 10 authores allow");
            return false;
	}   //addAffiliation
 
	var newauthorDiv = $(document.createElement('div')) 
	     .attr("id", 'authorDiv' + counter);
 
	newauthorDiv.after().html('<label> <b> Author '+ counter + ' </b> </label>' +
		'<br><label>Name : </label>' +
	      '<input type="text" name="author1_name' + counter + '" id="author1_name' + counter + '" value="" >'+
		  '<div id="AffiliationGroup"><label>Affiliation 1 : </label>' +
	      '<input type="text" name="author' + counter + 'affiliation' +  counter +
	      '" id="author' + counter + 'affiliation' +  counter +'" value="" >' + 
		  '<input type="button" id="addAffiliation" value="+" >' +
		  '<input type="button" id="removeAffiliation" value="-" >' + '</div>');
 
	newauthorDiv.appendTo("#AuthorGroup");
 
 
	counter++;
     });
 
     $("#removeAuthor").click(function () {
	if(counter==1){
          alert("No more author to remove");
          return false;
       }   
 
	counter--;
 
        $("#authorDiv" + counter).remove();
 
     });
 
     $("#getButtonValue").click(function () {
 
	var msg = '';
	for(i=1; i<counter; i++){
   	  msg += "\n Author " + i + " : " + $('#author' + i).val();
	}
    	  alert(msg);
     });
  });
  
  
  // Affiliation 
  
  $(document).ready(function(){
 
    var counter = 2;
 
    $("#addAffiliation").click(function () {
 
	if(counter>10){
            alert("Only 10 Affiliations allow");
            return false;
	}   
 
	var newTextBoxDiv = $(document.createElement('div'))
	     .attr("id", 'TextBoxDiv' + counter);
 
	newTextBoxDiv.after().html('<label> Affiliation '+ counter + ' : </label>' +
	      '<input type="text" name="author1_affiliation' + counter + 
	      '" id="author1_affiliation' + counter + '" value="" >');
 
	newTextBoxDiv.appendTo("#AffiliationGroup");
 
 
	counter++;
     });
 
     $("#removeAffiliation").click(function () {
	if(counter==1){
          alert("No more Affiliations to remove");
          return false;
       }   
 
	counter--;
 
        $("#TextBoxDiv" + counter).remove();
 
     });
 
     $("#getButtonValue").click(function () {
 
	var msg = '';
	for(i=1; i<counter; i++){
   	  msg += "\n Affiliation " + i + " : " + $('#author1_affiliation' + i).val();
	}
    	  alert(msg);
     });
  });
  
</script>
</head><body>
 
<div id='AuthorGroup'>

		<label><b>Author 1 </b></label> <br>
        <label>Name : </label><input type='author' id='author1_name1' >
       
        <div id='AffiliationGroup'>
		 <label>Affiliation 1 : </label><input type='textbox' id='author1_affiliation1' > 
        <input type='button' value='+' id='addAffiliation'>
        <input type='button' value='-' id='removeAffiliation'>
        <!--<input type='button' value='Get TextBox Value' id='getButtonValue'>-->
		</div>    
</div>
<input type='button' value='Add Author' id='addAuthor'>
<input type='button' value='Remove Author' id='removeAuthor'>
<!--<input type='button' value='Get author Value' id='getButtonValue'>-->
 
</body>
</html>

最佳答案

参见http://jsfiddle.net/9y3n33jx/

我还没有完成整个事情,但它应该会给你一个想法。

您需要将原始作者放入 div.authorDiv 中。对于 + - 按钮使用类名而不是 id。

    <input type='button' value='+' class='addAffiliation'>
    <input type='button' value='-' class='removeAffiliation'>

因为这些按钮将动态添加,所以您需要绑定(bind)到按钮类的 body 上的单击事件:

$('body').on("click", ".addAffiliation", function () {

(单击按钮时,您需要按类别计算出 div 内的附属机构数量,即 $(this).parent().(".affiliateClass").length)

当单击添加按钮时,需要将新的 div 添加到单击按钮的父级:

newTextBoxDiv.appendTo($(this).closest(".authorDiv"));

您必须对 - 按钮执行相同的操作。这应该会让你朝着正确的方向前进。

关于javascript - 在按钮 onclick 中创建动态文本框并将数据保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26354459/

相关文章:

php - 最佳实践ajax随机加载更多图像(内容)

php - laravel 使用 eloquent 填充选择框和 Blade

javascript - 如何将实际函数分配给 javascript 对象以用作回调?

javascript - 来自鼠标点的 jQuery 位置元素(以及更多)

javascript - 使用 Javascript 验证动态生成的表单

javascript - react native : Calling setState in FlatList much slower than setState in ScrollView

javascript - Javascript 中的原型(prototype)

javascript - 如何在 Bootstrap Accordion 中选择值

javascript - ngModel 未在 Internet Explorer 中正确更新 - AngularJS 1.3

尝试将文件上传到服务器时 PHP SSH2 操作失败