开发者

conditionally add elements to jquery array from partialview

开发者 https://www.devze.com 2023-03-28 23:36 出处:网络
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 parti

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

0

精彩评论

暂无评论...
验证码 换一张
取 消