문제

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