简体   繁体   English

使用jquery标记一个复选框

[英]Label a checkbox using jquery

i'm not familiar with jquery. 我不熟悉jquery。

What i'm trying to do is, drag and drop a text which will be displayed as a checkbox and the checkbox should have the id and tooltip same to the dropped-text. 我要做的是,拖放一个文本,该文本将显示为一个复选框,复选框应该具有与删除文本相同的ID和工具提示。 For that i used the following code.but it's not working.please help 为此我使用了以下代码。但它不起作用。请帮助

 <script type="text/javascript">

  $(init);

  function init() {




  function addColumn(column) 
  {
    var iHtml;


    //Labeling and Tool Tip the Checkbox



   iHtml = "<span title='ToolTipText'>"+
   "<input id='<%" + column + ".ClientID%>' type='checkbox' name='<%" + column + ".ClientID %>' />"+
    "<label for='<%" + column + ".ClientID%>'>MyCheckBox</label></span>";

    return iHtml
  }

There are lots of problems with that code. 该代码存在许多问题。 For one thing, although HTML5 allows just about any character in an id other than a space, earlier versions of HTML were more restrictive and CSS still is , so you really don't want to start an id with a # . 首先,尽管HTML5允许 id除空格之外的任何字符,但HTML的早期版本更具限制性,CSS仍然存在 ,所以你真的不想用#开始id (You can avoid using an id entirely in this case, unless you need the id for code you haven't quoted.) Your input type has a typo ("checbox") and so it won't be a checkbox, it'll default to "text". (在这种情况下你可以避免完全使用id ,除非你需要你没有引用的代码的id 。)你的input类型有一个拼写错误(“checbox”),所以它不会是一个复选框,它会默认为“文字”。

Here's my best guess at what you want: 这是我对你想要的最好的猜测:

iHtml = "<label title='ToolTipText'>" +
        "<input id='x<%” + column + ".ClientID%>' type='checkbox' name='x<%” +column+".ClientID%>'/>" +
        " MyCheckBox</label>";

...but I'm not at all sure your ASP.Net aspects in there are right (I've left it as you had it), looks dodgy to me but I don't do a lot of ASP.Net. ...但我完全不确定你的ASP.Net方面是否正确(我已经离开了它),看起来很狡猾,但我没有做很多ASP.Net。 (Not least the fancy character.) (尤其是花哨的角色”。)

What I did above: 我上面做了什么:

  • Make the id and name start with an x and got rid of the # . 使idnamex开头并删除# See the links above for why. 请参阅上面的链接了解原因。 (I left the id because I wasn't sure you didn't need it; if you don't need it, if the name is enough, you can remove it.) (我离开了id因为我不确定你不需要它;如果你不需要它,如果name足够,你可以删除它。)
  • Put the input inside the label . input 放在 label When you do that, you can avoid the whole for thing. 当你这样做时,你可以避免整体for事情。
  • Put the title on the label , no need for an extra span . title放在label ,无需额外的span
  • Corrected the type . 更正了type
  • Put the text of all attributes in single quotes. 将所有属性的文本放在单引号中。 This is largely stylistic, at least in HTML (quoting attriutes is not stylistic in XHTML, it's required). 这在很大程度上是风格的,至少在HTML中(引用attriutes在XHTML中不是风格,它是必需的)。
<input type="checkbox" id="blablabla" value="myvalue" class="myclass"/> 

<script type="text/javascript">
$(document).ready(function(){

$("#blabla").attr({title: "mytooltip"})
            .before(function(){
                      return "<label for=" + this.id +">MyLabelText</label>"
            });
});
</script> 

or short form 或简短形式

$(
$("#blabla").attr({title:"mytooltip"})
            .before("<label for='blabla'>MyLabelText</label>");
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM