- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个输入框,用户可以在其中指定要动态添加的新 DIV
元素的数量,该请求将通过 ajax 发送到 php,并在其中准备一个 html DIV
> 使用一些内部 input
标签,每个 DIV
具有使用用户提供的数字以增量方式生成的唯一 id
属性,并将其发送回 JS,现在 JS 将这个新的 html 附加到现有的 form
中。
我还为每个新的 DIV
提供了一个删除按钮,用户可以通过该按钮删除任何动态添加的 DIV
。
用户可以随时添加和删除元素。很多时候,我不想使用数据库来重新编码添加或删除的元素数量。
现在我需要一些解决方案,我可以在其中创建和分配唯一的 ids
到 div
并在那里保留一些记录,以便在提交表单时我可以循环遍历这些记录并从这些 DIV
中获取用户提交的数据。
第一个 HTML 代码:
<tr><td>No. Of Hotel</td><td><input type="text" id="noofhotel" name="noofhotel">
<input class="btn green" type="button" value="Submit" onclick="get_multiple_hotel_div()"></td></tr>
第二个 JS 代码
function get_multiple_hotel_div(){
var hotel_no = $("#noofhotel").val();
input_form_data = {
hotel_no : hotel_no
}
$.ajax({
type : "POST", //TODO: Must be changed to POST
data : input_form_data,
url: 'index.php?/process_transcation/get_multiple_hotel_div',
beforeSend : function(){
$('#loading_div').html("<img src='public/images/loading_red.gif' style='margin-left:50%; margin-top:5%;'>");
},
success : function(data_response){
$('#loading_div').empty();
data_response = $.parseJSON(data_response);
$('#multi_hotel_table').append(data_response.div_form); } }); }
第三个 PHP 代码生成 DIV
function get_multiple_hotel_div($hotel_no){
for($i=1;$i<=$hotel_no;$i++){
$hotelinput_form.="<div class='subtask_input_form_div multi_hotel_div' id='hoteldiv_".$i."'><p class='basic_eq_div_p'>Hotel Entry     <input type='button' id='remove' name='remove' value='Remove' class='btn green' onclick='remove_div(\"hoteldiv_$i\")'></p>
<table>
<tbody>
<tr>
<td style='display:none;'><input type='hidden' id='hotelid_".$i."' name='mhotelno[]'></td>
</tr>
<tr>
<td class='headtd'>Hotel Name</td>
<td class='headtd'><input type='text' id='mhotelname_".$i."' class='mhotel' name='mhotelname_".$i."' style='width:90% !important;' onkeyup='search_dropdown_data(this.value,this.id,\"hotel_table_$i\",\"mhotelid_$i\",\"$supplier_master_table\")'><div class='dropdown_div'><table id='hotel_table_".$i."' class='dropdown_table'></table></div></td>
<input type='hidden' id='mhotelid_".$i."' name='mhotelid_".$i."' class='mhotel'>
<td class='headtd'>Destination </td>
<td class='headtd'><input type='text' id='mdestination_".$i."' class='mdestination' name='mdestination_".$i."' style='width:90% !important;' onkeyup='search_dropdown_data(this.value,this.id,\"rt_to_table_$i\",\"mdestinationid_$i\",\"$destination_master_table\")'><div class='dropdown_div'><table id='rt_to_table_".$i."' class='dropdown_table'></table></div></td>
<input type='hidden' id='mdestinationid_".$i."' name='mdestinationid_".$i."' class='mdestination'>
</tr></tbody>
</table>
</div>";
}// End of for loop
$response_array = array("div_form"=>$hotelinput_form);
return json_encode($response_array);
}//end of function
第四个 JS 代码删除 div
function remove_div(div_id){
$("#"+div_id).remove();}
最佳答案
在此处查看解决方案:DEMO
HTML:您需要从用户处获取请求的 div 数量,并将它们放入容器中,并记住表单内的 div 列表。因此,您需要一个输入字段、一个按钮、一个容器和一个隐藏字段。
<input name="nr_divs" type="text" maxlength="2" />
<button id="b-new-div">+</button>
<form>
<div id="container"></div>
<input type="hidden" id="div_id_list" name="div_id_list" value="" />
</form>
jQuery:您需要在本地创建 div(函数 1),然后跟踪它们的 id(函数 2)。您还需要两个全局变量:一个用于递增 div 的 id(计数器),另一个用于存储所有可用的 id(数组)。
var nr_div=0; // the counter
var div_list=[]; // the storage
function updateDivList(id, action) {
/*
id = the div id
action= add or remove a div from the list
*/
if (action>0) { // add new div in the list
div_list.push(id);
} else { // remove a div from the list
var temp=[];
for (var i=0; i<div_list.length; i++) {
if (div_list[i]!=id) {
temp.push(div_list[i]);
}
}
div_list=temp;
}
// Put the div list in the hidden field, inside the form
$("#div_id_list").val(div_list.join(","));
}
function newDiv(container, div_query) {
/*
container = where does the new div go?
div_query = how many divs?
*/
var html="<div div-id=\""+nr_div+"\" >";
html+="<button class=\"b-remove\" type=\"button\">remove</button>";
html+="/* Your HTML code goes here*/";
html+="</div>";
$(container).append(html); // the div appears in the container
updateDivList(nr_div,1); // add the new div in the list
nr_div++; // increase the counter
if (div_query-1 > 0) { // if more divs are needed, recall function
newDiv(container, div_query-1);
} else { // add remove div action to the inside button
$(".b-remove").click(function(){
updateDivList($(this).parent().attr("div-id"),-1);
$(this).parent().remove();
});
}
}
为根据用户输入生成 div 的按钮创建操作:
$("#b-new-div").click(function(){
var nr=$("input[name='nr_divs']").val();
if (nr.length>0 && !isNaN(nr)) {
newDiv("#container",nr);
}
$("input[name='nr_divs']").val("");
});
因此,您可以多次更新 div,无需使用 Ajax/PHP。您还可以获得新 div 的唯一 ID。最后,隐藏输入字段内有 div 列表,该列表在表单中传递。
诗。您也可以只在本地处理表单,然后通过 Ajax 将最终结果发送到 PHP。
旧答案:
您可以将所有 id 存储在一个数组中。然后每当删除或添加 div 时更新数组。当您发送表单时,您也可以简单地传递数组,以便服务器可以从数组中读取 id,然后检查输入。 (您可以在 Ajax 请求中发送数组,或者将其放入表单内的隐藏输入中)
(我还想问,如果没有ajax和php,在本地创建de div不是更容易吗?似乎有很多来回请求。)
诗。如果客户反复要求更多的 div 会发生什么?表单中会有唯一的 id 吗?因为PHP文件似乎总是会生成从1开始到酒店编号的id。
关于javascript - 如何使用 javascript 动态添加 html 元素并在 php 中保存它们的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31635932/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!