简体   繁体   English

如何在asp.net母版页中添加和删除输入文本框

[英]how to add and remove input textbox in asp.net master page

*trying to add and remove input textbox for a form cv but it does not work. *试图添加和删除表单 cv 的输入文本框,但它不起作用。 by using asp.net in master page.通过在母版页中使用 asp.net。 when I click in icon doesn't work.当我点击图标时不起作用。 it will be a textbox and icon "+" to add .. when click in the icon it give the same textbox input in. and other icon to remove the other textbox..它将是一个文本框和图标“+”来添加 .. 当点击图标时,它会提供相同的文本框输入。和其他图标以删除其他文本框..

I'm not use MVC.. if there is other way to code?我不使用 MVC ..如果有其他编码方式吗? I will appreciate this.我会很感激的。

this is my code: enter code here this is my code (java script+css+html)这是我的代码: enter code here这是我的代码 (java script+css+html)

 $(document).ready(function () { var counter = 2; $("#addButton").click(function () { if (counter > 10) { alert("Only 10 textboxes allow"); return false; } var newTextBoxDiv = $(document.createElement('span')) .attr("id", 'TextBoxDiv' + counter); newTextBoxDiv.after().aspx('Textbox' + counter + '<input type="text" name="courseName' + counter + '" id="courseId' + counter + '" value="" >'); newTextBoxDiv.appendTo("#course"); counter++; }); $("#removeButton").click(function () { if (counter == 1) { alert("No more textbox to remove"); return false; } counter--; $("#TextBoxDiv" + counter).remove(); }); $("#sumbitcvId").click(function () { var msg = ''; for (i = 1; i < counter; i++) { msg += "\\n Textbox #" + i + " : " + $('#textbox' + i).val(); } alert(msg); }); });
 .coursecv,.mosdcv { font-family: 'Assistant', sans-serif; padding: 10px; margin: 10px; width: 27.5%; position: relative; font-size: 14px; color: #555; background-color: #fff; border: 1px solid #ccc; display: inline-block; }
 <div id="course"> <div> <h1>course</h1> </div> <span id="TextBoxDiv"> <asp:TextBox ID="courseId" runat="server" name="courseName" type="Text" placeholder="course name" CssClass="coursecv" value="" required="required"></asp:TextBox> </span> <span> <asp:TextBox ID="mosdId" runat="server" name="mosdName" type="Text" placeholder="Corporation" CssClass="mosdcv" required="required"></asp:TextBox> </span> </div> <div Id="ADD" style="margin-left:87%;color: #3c6bf4;font-family: 'Assistant', sans-serif;font-size: 17px;"> <a href="javascript:void(0);" class="addButton" title="Add field"><i class="fas fa-plus-circle"></i></a> <a href="javascript:void(0);" class="removeButton" title="Remove field"><i class="fas fa-minus-circle"></i></a> </div>

Since you're using class names to identify the fields in JQuery - you should use the dot(.) as the identifier than hash (#).由于您使用类名来标识 JQuery 中的字段 - 您应该使用点 (.) 作为标识符而不是哈希 (#)。 Hash is used to identifying elements using Id.哈希用于使用 Id 标识元素。

$(".addButton").click(function () {

    if (counter > 10) {
        alert("Only 10 textboxes allow");
        return false;
    }

    var newTextBoxDiv = $(document.createElement('span'))
        .attr("id", 'TextBoxDiv' + counter);

    newTextBoxDiv.after().append('Textbox' + counter +
        '<input type="text" name="courseName' + counter +
        '" id="courseId' + counter + '" value="" >');

    newTextBoxDiv.appendTo("#course");


    counter++;
});

The above code will trigger the click action and will add text boxes.上面的代码将触发点击动作并添加文本框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM