Javascript 如何将所需的输入字段放入Js代码中
如何将必填字段放入我的Javascript 如何将所需的输入字段放入Js代码中,javascript,jquery,Javascript,Jquery,如何将必填字段放入我的Jscode 我在xml视图中设置了required=true,但它确实阻止了所有表单 如何为jscodejQuery 这是我的jQuery代码: // table course jQuery(document).ready(function() { var id = 0; var cr = 0; jQuery("#addcourserow").click(function() { id++;
Js
code
我在xml
视图中设置了required=true
,但它确实阻止了所有表单
如何为js
codejQuery
这是我的jQuery代码:
// table course
jQuery(document).ready(function() {
var id = 0;
var cr = 0;
jQuery("#addcourserow").click(function() {
id++;
var row = jQuery('.courserow tr').clone(true);
var c = 1;
row.find("input").each(function(){
if (c === 1) {
$(this).attr('name','course_name_'+id);
}
else if (c === 2) {
$(this).attr('name','course_duration_'+id);
}
else if (c === 3) {
$(this).attr('name','course_date_'+id);
}
c++;
});
row.appendTo('#CourseTable');
return false;
});
$('.remove').on("click", function() {
$(this).parents("tr").remove();
});
});
这是我的XML
<!-- Course -->
<table id="CourseTable">
<thead>
<th>name</th>
<th>duration</th>
<th>date</th>
</thead>
<tr id="tr_course">
<td><input type="text" name="course_name_1" id="course_name"/></td>
<td><input type="text" name="course_duration_1" id="course_duration"/></td>
<td><input type="date" name="course_date_1" id="course_date" /></td>
<td><button class="remove">Remove</button></td>
</tr>
</table>
<input type="button" id="addcourserow" value="add row" />
<table class="courserow" style="display:none">
<tr>
<td><input type="text" id="course_name" /></td>
<td><input type="text" id="course_duration"/></td>
<td><input type="date" id="course_date"/></td>
<td><button class="remove">Remove</button></td>
</tr>
</table>
</div>
名称
期间
日期
去除
去除
<>我在 中添加了以下代码:
$(函数(){
var-id=0;
var-cr=0;
变量名称=[
“课程名称”,
“课程持续时间”,
“课程日期”
];
$(“#addcourserow”)。单击(函数(){
var row=$('#课程行模板tr').clone(true);
id++;
var c=0;
行。查找(“输入”)。每个(函数(){
var inpId=$(this).attr(“id”)+“”+id;
$(this.attr)({
id:inpId,
名称:名称[c++]+“389;”+id
}).prop(“必需”,真实);
log($(this));
});
行。附加到(“#课程表”);
返回false;
});
$('.remove')。在(“单击”,函数()上){
$(this.parents(“tr”).remove();
});
});代码>
名称
期间
日期
去除
去除
我找到了答案
jQuery(document).ready(function() {
var id = 0;
jQuery("#addcourserow").click(function() {
id++;
var row = jQuery('.courserow tr').clone(true);
var c = 1;
row.find("input").each(function(){
if (c === 1) {
$(this).attr('name','course_name_'+id).prop("required", true);
}
else if (c === 2) {
$(this).attr('name','course_duration_'+id).prop("required", true);
}
else if (c === 3) {
$(this).attr('name','course_date_'+id).prop("required", true);
}
c++;
});
row.appendTo('#CourseTable');
return false;
});
$('.remove').on("click", function() {
$(this).parents("tr").remove();
});
});
我在Xml中添加了required
<table id="CourseTable">
<thead>
<th>name</th>
<th>duration</th>
<th>date</th>
</thead>
<tr id="tr_course">
<td><input type="text" name="course_name_1" id="course_name" required="required"/></td>
<td><input type="text" name="course_duration_1" id="course_duration" required="required"/></td>
<td><input type="date" name="course_date_1" id="course_date" required="required"/></td>
<td><button class="remove">Remove</button></td>
</tr>
</table>
名称
期间
日期
去除
I在xml视图中设置required=true…..您没有设置任何!!如果前一行值为空,是否要阻止添加新行?请尝试使用代码:您可以使用prop实现此目的:$(“#elementId”).prop(“必需”,true);这可能有助于感谢你们所有人,我知道prop('required',true);但我不知道我把Jquary放在哪里了code@developer不清楚需要哪些项目。这应该在模板中或jQuery中的其他地方。如果它们都是必需的,那么需要在each()
loop.hi@Twisty中添加4d。谢谢你的回答,但我需要每个输入的不同名称,这就是为什么我有插入条件If(你可以在codepen中看到[我不知道的问题或插入inputs@developer我更新了我的答案。在提交表单时,如果name
属性不存在,则使用id
。如果您需要包含名称,则现在包含名称。请记住id
也应该是唯一的。