【发布时间】:2026-01-07 07:15:02
【问题描述】:
请帮助我。 我有一个包含多个文本输入的表单,这些输入具有不同的 id 但与下面的类相同
<input id="user_1" type="text" class="user-list" />
<input id="user_2" type="text" class="user-list" />
<input id="user_3" type="text" class="user-list" />
现在使用类定义,我正在调用 jquery 对话框,如下所示
$('.user-list').click(function(){
$( "#user-form" ).dialog( "open" );
tableOpen(this);
});
这里的用户表单是下面的名称列表
<div id="user-form" title="Select Employee" >
<table id="user-table">
<thead><th>Employee No</th><th>Name</th></thead>
<tbody>
<tr><td>Employee-1 No</td><td>Employee-1 Name</td></tr>
<tr><td>Employee-1 No</td><td>Employee-1 Name</td></tr>
<tr><td>Employee-1 No</td><td>Employee-1 Name</td></tr>
</tbody>
</table>
</div>
现在我使用对话框在对话框中打开 div 元素
$( "#user-form" ).dialog({
create: function() { },
autoOpen: false,
cache: false,
closeOnEscape: true,
height: 400,
width: 600,
modal: true,
buttons: {
Cancel: function() { $( this ).dialog( "close" );}
},
close: function() {}
});
对话框按预期出现。现在我在 tableOpen 函数的用户表行中添加一个点击事件,用于选择用户。
function tableOpen(selInput)
{
alert($(selInput).id().text()+"first");
$('#user-table tr').click( function() {
alert($(selInput).id().text()+"second");
});
//some other function here from displaying the selected user to input field
}
问题从这里开始。我第一次通过单击选择用户时,tableOpen 函数中的两个警报都只执行一次。但是在随后单击时,第一个警报仅显示一次,但第二个警报还会记住以前的单击,并且我之前单击的所有时间都会显示警报。
【问题讨论】:
-
您的 javascript 与 html 是否在同一个文件中?我有这个完全相同的问题。在我将所有 javascript 代码移动到 .js 文件后,它已修复。
标签: javascript jquery html