English 中文(简体)
如何从html投入中确定价值?
原标题:How do i get attribute values from html inputs?

I have a radio button that looks like this:

<input class="MCQRadio" id="MCQ" mcq-id="1" name="MCQ" question-id="1" type="radio" value="MCQ" /> MCQ

<><>>

When i click the radiobutton, and several radio buttons have the same class name, how do i retrive the question-id value and mcq-id value from the specific button? Preferably with jQuery.

我已经设立了这样一个点击手:

$(document).ready(function () {

    $( .MCQRadio ).click(function () {
最佳回答

所提供的大多数答复都是正确的。 我将作一补充,避免两度进入OM,以找到你的无线电台。

//Store the jQuery object in a variable so that it s only retrieved once
var $myRadio = $( #MCQ );

//Get your attributes
var mcq-id = $myRadio.attr("mcq-id");
var question-id = $myRadio.attr("question-id");

也可附上所有无线电台的点击事件(按班级挑选)和在事件手里挖掘特性:

$( .MCQRadio ).click(function() {
    //Use $(this) to grab the attribute value of the selected element
    var mcq-id = $(this).attr( question-id );
    var question-id = $(this).attr( mcq-id );
});
问题回答
$( #MCQ ).attr( question-id )
$( #MCQ ).attr( mcq-id )
var mcq-id = $( #MCQ ).attr("mcq-id");
var question-id = $( #MCQ ).attr("question-id");

The other answers already show you how to get the particular attributes. But since you added in your edit that you want to get the attributes from the particular one that was clicked you can try:

$( .MCQRadio ).click(function() {

   $(this).attr( question-id );
   $(this).attr( mcq-id );

});

These will return the values you want.

$( #MCQ ).attr( mcq-id )
$( #MCQ ).attr( question-id )




相关问题
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.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签