conditionally add elements to jquery array from partialview
I am loading one of several partialviews depending on a selection that's made. Inside those partial views I need to capture all o开发者_StackOverflowf the form elements that are included on that partialview inside a jquery array.
currently I'm using this:
var allInputs = $('input[data-var]').add('select[data-var]').add('textarea[data-var]');
but that also captures all of the form elements from the parent view. Is there a way to narrow the scope to only grab the elements on "this" partial view?
Is your partial view rendered within a specific element? If not, you could modify the partial view to wrap all elements and then scope your jQuery selector to that element:
Partial View:
<div id="myPartialView">
<!-- inputs here .... -->
</div>
Then, your javascript would be as follows:
var allInputs = $('input[data-var]').add('select[data-var]', '#myPartialView').add('textarea[data-var]', '#myPartialView');
Take a look at the jQuery docs for more on selector context: http://api.jquery.com/jQuery/#jQuery1
精彩评论