单击文本框时的MVC Ajax请求?

时间:2022-10-07 17:57:27

Im using MVC2, im trying to basically fire an ajax request. Im basically trying to get a specific partial view to load in a div when the user selects a textbox.

我在使用MVC2,我试图启动一个ajax请求。当用户选择一个文本框时,我基本上是在尝试获取一个特定的局部视图来加载div。

(to provide background.. the textboxes are for users to input measurements, i have a div that contains a partial view that returns instructions on how to measure. id like them to load when the user clicks on the the textbox to input their measurements)

(提供背景. .文本框是供用户输入测量值的,我有一个div,它包含一个部分视图,该视图返回关于如何度量的说明。当用户点击文本框输入测量值时,我希望他们加载)

Is this possible? ive used ajax.actionlink's elcewhere in my site, but im not too sure on what to research.. as im reasonably sure ajax.actionlinks wont be the tool that i need for this job.

这是可能的吗?我使用ajax。actionlink在我的网站上的elcewhere,但是我不太确定要研究什么我相当肯定阿贾克斯。actionlinks不是我做这份工作所需要的工具。

Ive been attempting to mess around with something like this

我一直在想弄点像这样的东西

$(function() {
$('#Chest').focus(function () {
$('#measureRight').load('@Url.Action("http://www.google.com")');
});
});

(in the above example 'Chest' is a textbox id and www.google.com is just to check that a request is being fired)

(在上面的例子中,“Chest”是一个文本框id, www。google。com只是为了检查是否有一个请求被触发)

any advice would be appriciated. im terribly new / totally new to jquery, so please be kind!

任何建议都会被采纳。我对jquery是全新的,请见谅!

Thanks

谢谢

1 个解决方案

#1


1  

I don't know if jQuery works differently in a MVC or ASP environment but this should work:

我不知道jQuery在MVC或ASP环境中是否有不同的工作方式,但这应该是可行的:

<input type="text" id="myField" />
<div style="display:none" id="myDiv"></div>

$(function() {

   $('#myField').focus(function() {

      $('#myDiv').load('/path/to/other/page').show();

   });

});

Whatever you have on this page '/path/to/other/page' will load inside your div. If you want to load only a portion of that page, then you can pass a second parameter (not sure if that's the right word in this case) identifying the element you want to grab content from, i.e. the id or class of the element on that page that you want to show like so:

无论你对这个页面/道路/ /其他/页面的加载在div。如果你想页面只加载的部分,然后你可以通过第二个参数(不知道这是正确的词在这种情况下)你想抓住内容识别元素,即,页面上的元素的id或类你想显示如下所示:

$('#myDiv').load('/path/to/other/page #showOnlyWhatsInHere');

#1


1  

I don't know if jQuery works differently in a MVC or ASP environment but this should work:

我不知道jQuery在MVC或ASP环境中是否有不同的工作方式,但这应该是可行的:

<input type="text" id="myField" />
<div style="display:none" id="myDiv"></div>

$(function() {

   $('#myField').focus(function() {

      $('#myDiv').load('/path/to/other/page').show();

   });

});

Whatever you have on this page '/path/to/other/page' will load inside your div. If you want to load only a portion of that page, then you can pass a second parameter (not sure if that's the right word in this case) identifying the element you want to grab content from, i.e. the id or class of the element on that page that you want to show like so:

无论你对这个页面/道路/ /其他/页面的加载在div。如果你想页面只加载的部分,然后你可以通过第二个参数(不知道这是正确的词在这种情况下)你想抓住内容识别元素,即,页面上的元素的id或类你想显示如下所示:

$('#myDiv').load('/path/to/other/page #showOnlyWhatsInHere');