在 MVC 2 应用程序中反复调用 jQuery 函数
我想在文本框获得焦点时调用操作方法,以从数据库中获取与该字段关联的模型对象的描述。
我有这个 jQuery 函数:
function getDescription() {
$('.hourInput').focus(function () {
var name = $(this).attr('name');
var url = '<%=Url.Action("GetDescription", "Timesheet") %>'
$.get(url, { name: name }, function (data) {
$('#description').val(data);
});
});
}
现在这个简单的测试操作方法:
public ActionResult GetDescription(string name)
{
return Content("Testing");
}
就获取测试文本而言,它工作得很好,但问题是,如果我在操作方法中设置断点,我会看到它被一遍又一遍地调用,不仅仅是当我在文本框中改变焦点时......
有人知道为什么吗?
I want to call an action method when an textbox gets focus to get a description of the model object that field is associated with from a database.
I have this jQuery function:
function getDescription() {
$('.hourInput').focus(function () {
var name = $(this).attr('name');
var url = '<%=Url.Action("GetDescription", "Timesheet") %>'
$.get(url, { name: name }, function (data) {
$('#description').val(data);
});
});
}
And this simple test action method for now:
public ActionResult GetDescription(string name)
{
return Content("Testing");
}
It works fine as far as getting the test text, but the problem is if I set a breakpoint in the action method, I see that it gets called over and over, not just when i change focus in the text boxes...
Anyone have any idea why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能听起来很愚蠢,但可能是您切换到 Visual Studio 并返回 IE 时导致焦点事件再次被触发?
另一件需要考虑的事情是,如果字段描述没有更改,您确实应该缓存响应。事实上,你真的需要这个动作吗?你不能在第一次渲染页面时写出字段描述吗?
This may sound stupid, but could it be where you are switching to Visual Studio and the back to IE you are causing the focus event to be triggered again?
The other thing to consider is that you should really cache the response if the field description doesn't change. In fact, do you actually need this action at all, can't you write out the field description when you're first rendering the page?