Pregunta

I have a webpage that i am converting to razor view engine. I have this code, which is failing now:

if ($("input[@name='exerciseRB']:checked").val() == 'New') {
    $("#newExercise").show();
    $("#existingExercise").hide();
}
else {
    $("#newExercise").hide();
    $("#existingExercise").show();
}

where it seems that it thinks the @ (in @name) is part of my mvc razor (as opposed to simply getting jquery selector. The error is "Can't resolve symbol name"

Is there anything suggestion here to allow jquery and razor to work nicely together?

¿Fue útil?

Solución

In this very specific case, you're using the XPath @ syntax for attributes, which has been deprecated in jQuery (being completely removed in jQuery ≥ 1.3, so it won't work in newer versions).

The solution here is to remove the @ and use the CSS attribute selector syntax:

if ($("input[name='exerciseRB']:checked").val() == 'New') {

If you need to escape any other raw @ symbols, double them so you get @@.

Otros consejos

I believe @@ would be how to escape the @ symbol in razor:

if ($("input[@@name='exerciseRB']:checked").val() == 'New') {
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top