English 中文(简体)
MVC3 html helpers and javascript
原标题:MVC3 html helpers and javascript

在MVC3中,我有一位html的助手,该助手将使用html的海关部分作为搜索控制。 问题是,已经撰写了ava(jquery)与控制互动(实际上应该利用JUST进行这种控制)。 是否有办法将javascript扎根,以便控制继续具有javascript功能。 (在创建助手时,我们可以控制html的结构,并方便撰写 j。) 这将使全线控制的使用标准化。

页: 1

<div>
@Html.SearchControl("searchControlSelector")
</div>

<script>
     $("searchControlSelector").timeout();
<script>

我想能够在打电话@Html时确定时间范围。 搜查(一些寄生虫)以便把 j和助手合并起来,帮助人的使用者不必担心他们在要求时间时应当使用什么选择人。 是否有任何人知道如何做到这一点? 是否有更好的处理方法?

最佳回答

It is hard to make completely reusable html helper with single shared javascript file.

首先,不可能只输出一个<代码><script>$(“searchControl”)、全时版(&t);/script>代码栏,而不必使用排外页上的其他方法。

You could add your own control-specific data-attribute like data-search-control and use it in selectors like $("input[data-search-control]").timeout() to distinct only HTML produced by your helper.

If it is ok for you to have multiple scripts block on page don t forget they will be executed few times so you need to care about preventing multiple execution. You may associate some data with HTML nodes already processed by the script using for example jQuery $("").data() method. Or as option you may check if specified object in global scope is declared and if it is then do nothing otherwise declare it and call your method.

问题回答

当我想做这样的事情时,我倾向于尽量简单。 在这里,一些法典与法案一样完善。

using System;
using System.Text;
namespace MvcApplication1.Helpers
{
    public class Html
    {
        public static string SearchControl(string selector)
        {
            StringBuilder returnString = new StringBuilder();
            //put your existing code here.
            returnString.Append(String.Format("<label >{0}</label>", selector));
            //just add the JS as string, (wrapped in a  ready  if you need that)
            returnString.Append(String.Format("<script>$(function(){$("{0}").timeout();});<script>", selector));
            return returnString.ToString();
        }
    }
}

If this isn t helpful, please add more description to your question and we ll try to help.





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签