简体   繁体   English

重构与值列表匹配的选择元素

[英]Refactor Select Elements Matching List of Values

I'm looking for some help on how to write some code to reduce redundant code while maintaining clarity. 我正在寻找有关如何编写一些代码以减少冗余代码同时保持清晰度的帮助。

Given this HTML (vastly simplified from the real thing): 有了这个HTML(从真实的东西大大简化了):

<div>
    <fieldset>
        <legend>Select Day for Code <span class="typeCode">089</span></legend>
        <select name="date1" id="date1" class="dateSelect">
            <option value="Mon">Monday</option>
            <option value="Tue">Tuesday</option>
            <option value="Wed">Wednesday</option>
            <option value="Thu">Thursday</option>
            <option value="Fri">Friday</option>
        </select>
    </fieldset>
    <fieldset>
        <legend>Select Day for Code <span class="typeCode">087</span></legend>
        <select name="date2" id="date2" class="dateSelect">
            <option value="Mon">Monday</option>
            <option value="Tue">Tuesday</option>
            <option value="Wed">Wednesday</option>
            <option value="Thu">Thursday</option>
            <option value="Fri">Friday</option>
        </select>
    </fieldset>
</div>

I am wanting to see which days were selected for specific codes. 我想查看为特定代码选择的日期。 Using jQuery, I could use something like this: 使用jQuery,我可以使用如下代码:

$(
    "div fieldset:has(.typeCode):contains(061) .dateSelect option:selected," +
    "div fieldset:has(.typeCode):contains(064) .dateSelect option:selected," +
    "div fieldset:has(.typeCode):contains(065) .dateSelect option:selected", +
    "div fieldset:has(.typeCode):contains(089) .dateSelect option:selected," +
    "div fieldset:has(.typeCode):contains(090) .dateSelect option:selected"
).each(
    function(index) {
        doSomethingHere();
    }
);

However, it seems like there ought to be a way to simplify that selector. 但是,似乎应该有一种简化选择器的方法。 Something like this would be nice, but it doesn't seem to be possible: 这样的事情会很好,但是似乎不可能:

$(
    "div fieldset:has(.typeCode):contains([061,064,065,089,090]) .dateSelect option:selected"
).each(
    function(index) {
        doSomethingHere();
    }
);

All I can come up with is something like this: 我所能想到的是这样的:

var typeCodes = ["061", "064", "065", "089", "090"];

$("div fieldset:has(.typeCode)").each(
    function(index) {
        var code = $(this).find(".typeCode").text();
        if ($.inArray(code, typeCodes) > -1) {
            var dateSelected = $(this).find(".dateSelect option:selected");
            doSomethingHere();
        }
    }
);

That eliminates duplicate code, but at the cost of reduced clarity. 这样就可以消除重复的代码,但是会降低清晰度。

Is there a better way to do something like this? 有没有更好的方法来做这样的事情?

I would throw typecode on each fieldset as a custom data-* attribute - filter out the ones you don't want - then check the select .. 我会扔typecode每个fieldset作为自定义data-*属性- filter掉不想要的-然后检查select ..

HTML: HTML:

<fieldset data-typecode="089">
    <legend>Select Day for Code <span class="typeCode">089</span></legend>
    <select name="date1" id="date1" class="dateSelect">
        <option value="Mon">Monday</option>
        <option value="Tue">Tuesday</option>
        <option value="Wed">Wednesday</option>
        <option value="Thu">Thursday</option>
        <option value="Fri">Friday</option>
    </select>
</fieldset>

And the JS: 和JS:

var typeCodes = ["061", "064", "065", "089", "090"];

$("fieldset[data-typecode]").filter(function() {
    //Get fieldsets with typecodes in above array and with selected options
    var code = $(this).data("typecode");
    return typeCodes.indexOf(code) > -1 && $(this).find("select option:selected").length
}).each(function() {
    //Do something on each fieldset
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM