- <h1 class="postTitle" style="margin-bottom: 10px; padding-left: 5px; font-size: 15.6px; border: 0px; float: left; width: 723.475px; clear: both; line-height: 1.5; color: rgb(0, 0, 0); font-family: "Helvetica Neue", Helvetica, Verdana, Arial, sans-serif; background-color: rgb(255, 255, 255);"><a id="cb_post_title_url" class="postTitle2 vertical-middle" href="https://www.cnblogs.com/Web-Architecture/p/6478371.html" style="outline: none; color: rgb(33, 117, 155); text-decoration-line: none; transition: all 0.4s linear 0s;"><span style="vertical-align: middle;">基于添加自定义字段的总结</span></a></h1><div class="clear" style="clear: both; color: rgb(0, 0, 0); font-family: "Helvetica Neue", Helvetica, Verdana, Arial, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"></div><div class="postBody" style="color: rgb(0, 0, 0); line-height: 1.7; font-family: "Helvetica Neue", Helvetica, Verdana, Arial, sans-serif; background-color: rgb(255, 255, 255);"><div id="cnblogs_post_body" class="blogpost-body" style="margin-bottom: 20px; word-break: break-word;"><p style="margin: 10px auto;">以下为添加自定义字段,设置动态添加表格行,删除表格行的实例。</p><p style="margin: 10px auto;">主要功能如下:</p><ul style="margin-left: 30px;"><li style="margin-bottom: 1em; margin-left: 0px; list-style-position: initial; list-style-image: initial;">添加行</li><li style="margin-bottom: 1em; margin-left: 0px; list-style-position: initial; list-style-image: initial;">删除行</li><li style="margin-bottom: 1em; margin-left: 0px; list-style-position: initial; list-style-image: initial;">选择当前字段名称</li><li style="margin-bottom: 1em; margin-left: 0px; list-style-position: initial; list-style-image: initial;">设置当前字段名称可编辑状态</li></ul><p style="margin: 10px auto;">图示为实现的演示图:</p><p style="margin: 10px auto;"><img src="https://images2015.cnblogs.com/blog/799972/201702/799972-20170228122317532-382270099.png" alt="" style="border: 0px; max-width: 700px; height: auto;"></p><p style="margin: 10px auto;"> </p><p style="margin: 10px auto;">html:</p><p style="margin: 10px auto;"> </p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: "Courier New" !important; font-size: 12px !important;"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="outline: none; color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 700px; height: auto;"></a></span></div><pre style="overflow: auto; font-family: "Courier New" !important;"> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">table </span><span style="color: rgb(255, 0, 0); line-height: 1.5 !important;">class</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">="table table-hidden"</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
- <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">thead</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
- <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">tr</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
- <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">th </span><span style="color: rgb(255, 0, 0); line-height: 1.5 !important;">colspan</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">="3"</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">a </span><span style="color: rgb(255, 0, 0); line-height: 1.5 !important;">id</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">="addRow"</span><span style="color: rgb(255, 0, 0); line-height: 1.5 !important;"> class</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">="btn btn-info"</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>添加一行<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">a</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">th</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
- <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">tr</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
- <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">tr</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
- <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">th</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>字段名称<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">th</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
- <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">th</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>字段值<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">th</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
- <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">th</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>操作<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">th</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
- <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">tr</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
- <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">thead</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
- <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"><</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">tbody </span><span style="color: rgb(255, 0, 0); line-height: 1.5 !important;">id</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">="optionContainer"</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
- <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">tbody</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span>
- <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;"></</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">table</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">></span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="outline: none; color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 700px; height: auto;"></a></span></div></div><p style="margin: 10px auto;">js:</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: "Courier New" !important; font-size: 12px !important;"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="outline: none; color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 700px; height: auto;"></a></span></div><pre style="overflow: auto; font-family: "Courier New" !important;"> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> File1(e){
- </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> console.log(e)</span>
- <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> name =<span style="line-height: 1.5 !important;"> e.id;
- </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> alert(name)</span>
- <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> value=name.substring(6,7<span style="line-height: 1.5 !important;">)
- </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> alert(value)</span>
- <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (e.value == '0'<span style="line-height: 1.5 !important;">) {
- $(</span>"#"+name).css('display', 'none'<span style="line-height: 1.5 !important;">);
- $(</span>"#"+name).siblings('input').attr('type', 'text'<span style="line-height: 1.5 !important;">);
- $(</span>"#"+name).siblings('input').attr('name', 'key_text_'+<span style="line-height: 1.5 !important;">value);
- }</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">else</span><span style="line-height: 1.5 !important;"> {
- alert(e.value)
- }
- }
- </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> Field2(e){
- </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> name =<span style="line-height: 1.5 !important;"> e.id;
- $(</span>"#"+name).siblings('select').css('display', 'block'<span style="line-height: 1.5 !important;">);
- $(</span>"#"+name).attr('type', 'hidden'<span style="line-height: 1.5 !important;">);
- }
- </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> rowCount = 0<span style="line-height: 1.5 !important;">;
- $(</span>"#addRow").click(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> () {
- </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">Field1(rowCount);</span>
- rowCount++<span style="line-height: 1.5 !important;">;
- </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> newRow = '<tr >' +
- '<td><select id="select' + rowCount + '" class="form-control FieldSelect">' +
- '<option value="1">QQ</option>' +
- '<option value="2">电话</option>' +
- '<option value="3">地址</option>' +
- '<option value="0">编辑</option></select>' +
- '<input id="input' + rowCount + '" type="hidden" name="key'+rowCount+'" class="form-control Fieldtxt" value="QQ" size="10" ondblclick="javascript:return Field2(this)"/> ' +
- '<td> <input class="form-control" placeholder="输入字段值" name="content'+rowCount+'" type="text"> </td> ' +
- '<td> <span class="delRow btn btn-danger" onclick="$(this).parent().parent().remove();">删除</span> </td> ' +
- '</tr>'<span style="line-height: 1.5 !important;">;
- $(</span>'#optionContainer'<span style="line-height: 1.5 !important;">).append(newRow);
- });</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="outline: none; color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 700px; height: auto;"></a></span></div></div><p style="margin: 10px auto;">以上实例在独立的文件中尚可实现,在开发过程中难免会遇到问题。</p><p style="margin: 10px auto;">把代码放进了项目中会显示 </p><pre style="overflow: auto;">Field()方法未定义的情况
- 就考虑了一种方案,在选择实现调用函数,并不写在动态标签中。
- 以下为解决方案:</pre><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: "Courier New" !important; font-size: 12px !important;"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="outline: none; color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 700px; height: auto;"></a></span></div><pre style="overflow: auto; font-family: "Courier New" !important;"><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> rowCount = 0<span style="line-height: 1.5 !important;">;
- $(</span>"#addRow").click(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> () {
- </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">Field1(rowCount);</span>
- rowCount++<span style="line-height: 1.5 !important;">;
- </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> newRow = '<tr >' +
- '<td>' +
- '<select id="select' + rowCount + '" name="key_select_'+rowCount+'" class="form-control FieldSelect">' +
- '<option value="1">QQ</option>' +
- '<option value="2">电话</option>' +
- '<option value="3">地址</option>' +
- '<option value="0">编辑</option>' +
- '</select>' +
- '<input id="input' + rowCount + '" type="hidden" name="key'+rowCount+'" class="form-control Fieldtxt" value="QQ" size="10" disabled="disabled"/> ' +
- '<td> <input class="form-control" placeholder="输入字段值" name="content'+rowCount+'" type="text"> </td> ' +
- '<td> <span class="delRow btn btn-danger" onclick="$(this).parent().parent().remove();" >删除</span> </td> ' +
- '</tr>'<span style="line-height: 1.5 !important;">;
- $(</span>'#optionContainer'<span style="line-height: 1.5 !important;">).append(newRow);
- $(</span>'#select' + rowCount).change(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> () {
- File1(</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">this</span><span style="line-height: 1.5 !important;">)
- });
- $(</span>'#input' + rowCount).dblclick(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> () {
- Field2(</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">this</span><span style="line-height: 1.5 !important;">)
- })
- });
- </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> File1=<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;">(e){
- </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">console.log(e);</span>
- <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">获取id</span>
- <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> name =<span style="line-height: 1.5 !important;"> e.id;
- </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">获取id在第几行</span>
- <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> value=name.substring(6,7<span style="line-height: 1.5 !important;">);
- </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">alert(value);</span>
- <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">判断值是否为编辑</span>
- <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (e.value == '0'<span style="line-height: 1.5 !important;">) {
- $(</span>"#"+name).attr("disabled","disabled"<span style="line-height: 1.5 !important;">);
- $(</span>"#"+name).css('display', 'none'<span style="line-height: 1.5 !important;">);
- $(</span>"#"+name).siblings('input').attr('type', 'text'<span style="line-height: 1.5 !important;">);
- </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">input 赋值name字段为key_text_**</span>
- $("#"+name).siblings('input').removeAttr('disabled'<span style="line-height: 1.5 !important;">);
- $(</span>"#"+name).siblings('input').attr('name', 'key_text_'+<span style="line-height: 1.5 !important;">value);
- }</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">else</span><span style="line-height: 1.5 !important;"> {
- </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">select选中的值</span>
- $("#"+name).attr('name','key_select_'+<span style="line-height: 1.5 !important;">value)
- }
- };
- </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> Field2=<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;">(e){
- </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">var</span> name =<span style="line-height: 1.5 !important;"> e.id;
- $(</span>"#"+name).siblings('select').removeAttr('disabled'<span style="line-height: 1.5 !important;">);
- $(</span>"#"+name).siblings('select').css('display', 'block'<span style="line-height: 1.5 !important;">);
- $(</span>"#"+name).attr('disabled', 'disabled'<span style="line-height: 1.5 !important;">);
- $(</span>"#"+name).attr('type', 'hidden'<span style="line-height: 1.5 !important;">);
- };</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="outline: none; color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 700px; height: auto;"></a></span></div></div><p style="margin: 10px auto;"> 以上为实现效果,当设置字段名称为输入框时,可双击返回选择下拉框来选择实现主要功能。</p></div></div>
复制代码 |