Closed tminglei closed 9 years ago
Any way I can get the expected result?
Cheerio doesn't implement that selector. From the jQuery documentation on the :radio
selector:
$( ":radio" )
is equivalent to$( "[type=radio]" )
...so you can try this substitution:
-$('[name=AgentNumber]:radio')[0].html()
+$('[name=AgentNumber][type=radio]')[0].html()
In general, it's a good idea to reference the jQuery documentation for ideas on alternatives.
I tried. Not work. Any other ideas?
I clearly wasn't paying enough attention. There are a few problems with your code:
$('[name=AgentNumber][type=radio]')[0].html()
When you de-reference the Cheerio object, you get a "node-like" object (just like with jQuery). This does not define an html
method, so that code should throw an error. Writing like this will avoid the error:
$('[name=AgentNumber][type=radio]')[0].html()
...but I don't think it will help you because the radio element doesn't have any HTML content. From the next line, it looks like you are trying to get the input value. You could re-write that as follows:
-form.data['AgentNumber'] = $('[name=AgentNumber]:radio')[0].value;
+form.data['AgentNumber'] = $('[name=AgentNumber][type=radio]').val();
Yes, I got the value by using your codes. Thanks so much! :+1:
But I still have one more little question:
why did I get just one value? shouldn't $('[name=AgentNumber][type=radio]')
return a set of results?
BTW, :radio
is also ok, using your updated codes $('[name=AgentNumber]:radio').val()
Anyway, it resolved my problem, so close it. Thanks!
Again, from the jQuery documentation (this time on .val
):
Get the current value of the first element in the set of matched elements
Glad it's working for you!
Got it. Thank you! :)
html
is for innerHTML
(and all input elements can't have children), [0].value
isn't implemented for DOM nodes; try .val()
.
Sorry for the repeated response. My browser didn't refresh, had this open from earlier.
Np. Anyway, @jugglinmike / @fb55, thanks again for your nice helping. :+1:
I'm using cheerio to parse and extract info from html, but seems I can't select the radios from it.
Here's my codes:
And here's the
res
content (sorry I pasted it here directly):When executing the codes, I got an empty result from
$('[name=AgentNumber]:radio')
.Did I used wrong way?
p.s. I'm using cheerio v0.19.0 on Windows 7 x64.