[英]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: 我上面做了什么:
id
and name
start with an x
and got rid of the #
. id
和name
以x
开头并删除#
。 See the links above for why. 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
足够,你可以删除它。) input
inside the label
. input
放在 label
。 When you do that, you can avoid the whole for
thing. for
事情。 title
on the label
, no need for an extra span
. title
放在label
,无需额外的span
。 type
. type
。 <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.