【发布时间】:2019-11-20 08:26:48
【问题描述】:
$('#btnAddPhysicians').click(function () {
var rowCount;
rowCount = $('#gvPhysicians tr').length;
if ($('#txtDoctorName').val() != '' && $('#gvPhysicians').length > 1) {
$('#gvPhysicians').after('<tr><td>' + rowCount + '</td>' +
'<td>' + $('#txtDoctorName').val() + '</td>' +
'<td>' + $('#txtSpecialty').val() + '</td>');
$('#divContainer').find('input:text').each(function () {
$('input:text[id=' + $(this).attr('id') + ']').val('');
}
);
}
else alert('Invalid!');
});
现在我在 jquery 中编写了这个函数,但它总是执行 else 语句“无效”
html文本框和gridview的代码如下
<div class="form-group">
<div class="tab-custom-content">
<label for="menu_name">Please list ALL active treating physicians (i.e. pulmonologist, oncologist, internist, cardiologist, etc)</label>
</div>
<div class=" row">
<div class="col-sm-4">
<label for="menu_name">Doctor’s Name</label>
<input type="text" class="form-control" id="txtDoctorName" name="txtDoctorName">
</div>
<div class="col-sm-4">
<label for="menu_name">Specialty</label>
<input type="text" class="form-control" id="txtSpecialty" name="txtSpecialty">
</div>
<div class="col-sm-4">
<br />
<button type="submit" id="btnAddPhysicians" class="btn btn-outline-primary">ADD</button>
</div>
</div>
<div class=" row">
<section class="content">
<div class="card">
<div class="card-header">
</div>
<!-- /.card-header -->
<div class="card-body">
<div id="gvPhysicians" class="jsgrid" style="position: relative; height: 100%; width: 200%;">
<div class="jsgrid-grid-header jsgrid-header-scrollbar">
<table class="jsgrid-table">
<tr class="jsgrid-header-row">
<th class="jsgrid-header-cell jsgrid-header-sortable" style="width: 400px;">Doctor's Name</th>
<th class="jsgrid-header-cell jsgrid-align-right jsgrid-header-sortable" style="width: 250px;">Specialty</th>
</tr>
</table>
</div>
<!-- /.card-body -->
<!-- /.card -->
</div>
</div>
</div>
</section>
</div>
<div class=" row">
<button type="submit" id="btnNext1" class="btn btn-outline-primary">Next</button>
</div>
</div>
我想在单击 btnAddPhysicians 时将 txtDoctorName 值和 txtSpecialty 显示到 gridview 列。我在 MVC 5 中执行此操作并为此使用 jquery。
【问题讨论】:
-
好吧,我看到 1 个问题
$('#gvPhysicians').length > 1意味着您应该有多个具有相同 ID 的元素,这是一个很大的禁忌 -
我是 jquey 和 MVC 的新手,我不知道如何解决这个问题
-
简单删除
$('#gvPhysicians').length > 1,拥有它没有多大意义,因为你的gvPhysicians是一个静态元素 -
我已经尝试过了,但它不起作用
-
它工作我只需添加 Length>=1 并且代码工作正常
标签: javascript jquery html asp.net asp.net-mvc