سؤال

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