简体   繁体   English

使用JavaScript选择跨度内的复选框

[英]Select Checkbox inside span using Javascript

I have a checkbox within a span container. 我在span容器中有一个复选框。 How can I get the type and productid using Javascript? 如何使用Javascript获取类型和产品ID?

Here's the innerHTML: 这是innerHTML:

<span productid="6">
    <input id="MainContent_chk8" name="ctl00$MainContent$chk8" type="checkbox">
    <label for="MainContent_chk8">Test6</label>
</span>

I'd do something as simple as this: 我会做些简单的事情:

var check = document.getElementById("MainContent_chk8"),
    productId = check.parentNode.getAttribute("productid");

Of course, this implies that you should know the id of the checkbox in advance - which may not be the case, as that seems to be a checkbox generated by the server. 当然,这意味着你应该知道该id的复选框提前-这可能并非如此,因为这似乎是由服务器生成的复选框。

It also implies that you are certain that the parent of the checkbox has the attribute you're looking for. 这也意味着您可以确定复选框的父项具有您要查找的属性。

It may help if you quote more of your code. 如果您引用更多代码,则可能会有所帮助。 Your question is tagget "asp.net", so I guess this HTML snippet is generated by some ASP.NET code. 您的问题是tagget“ asp.net”,所以我猜这个HTML代码段是由某些ASP.NET代码生成的。

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

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