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?

有帮助吗?

解决方案

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 @@.

其他提示

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

if ($("input[@@name='exerciseRB']:checked").val() == 'New') {
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top