根据 jQuery 中单选按钮选择的数量显示/隐藏 1-5 个输入框,需要清理

发布于 2024-10-22 00:20:26 字数 3345 浏览 1 评论 0原文

我已经编写了一些可以完美运行的代码,但它确实是丑陋的代码,我想学习清理它。

有没有人有比这个方法更好的建议?我使用了一些属性选择器来减少重复,但我仍然需要为每个硬件分配一个巨大的块(现在有 4 个)。我想我可以使用带有硬件变量的循环吗?

$('input[name="hwA-qty"]').click(function(){
    var selected = $(this).val();
    if (selected == 1) { //Hide inputs 2-5
        $('fieldset[id^="hwA"]').hide(); 
    }
    if (selected == 2) { //Show inputs 1 & 2
        $('fieldset#hwA-2').show();
        $('fieldset#hwA-3').hide();
        $('fieldset#hwA-4').hide();
        $('fieldset#hwA-5').hide();
    }
    if (selected == 3) { //Show inputs 1-3
        $('fieldset#hwA-2').show();
        $('fieldset#hwA-3').show();
        $('fieldset#hwA-4').hide();
        $('fieldset#hwA-5').hide();
    }
    if (selected == 4) { //Show inputs 1-4
        $('fieldset#hwA-2').show();
        $('fieldset#hwA-3').show();
        $('fieldset#hwA-4').show();
        $('fieldset#hwA-5').hide();
    }
    if (selected == 5) { //Show all inputs
        $('fieldset[id^="hwA"]').show();
    }
});
$('input[name="hwB-qty"]').click(function(){
    var selected = $(this).val();
    if (selected == 1) {
        $('fieldset[id^="hwB"]').hide();
    }
    if (selected == 2) {
        $('fieldset#hwB-2').show();
        $('fieldset#hwB-3').hide();
        $('fieldset#hwB-4').hide();
        $('fieldset#hwB-5').hide();
    }
    if (selected == 3) {
        $('fieldset#hdPvr2').show();
        $('fieldset#hdPvr3').show();
        $('fieldset#hdPvr4').hide();
        $('fieldset#hdPvr5').hide();
    }
    if (selected == 4) {
        $('fieldset#hdPvr2').show();
        $('fieldset#hdPvr3').show();
        $('fieldset#hdPvr4').show();
        $('fieldset#hdPvr5').hide();
    }
    if (selected == 5) {
        $('fieldset[id^="sdPvr"]').show();
    }
}); [...]

这是 HTML:

[...]
<div class="hardwareValidation">

<div class="select">
    <label for="hwA-qty">Quantity</label><br />
    <label>1 <input type="radio" name="hwA-qty" value="1" checked /></label>
    <label>2 <input type="radio" name="hwA-qty" value="2" /></label>
    <label>3 <input type="radio" name="hwA-qty" value="3" /></label>
    <label>4 <input type="radio" name="hwA-qty" value="4" /></label>
    <label>5 <input type="radio" name="hwA-qty" value="5" /></label>
</div>

<fieldset id="hwA-1">
    <div class="input">
        <label for="hwA-1-serial">#1 - Box Serial Number</label><br />
        <input type="text" name="hwA-1-serial" id="hwA-1-serial" />
    </div>
    <div class="checkbox">
        <label>
            <input type="checkbox" name="hwA-1-override" value=""> Override Validation
        </label>
    </div>
</fieldset>                     

<fieldset id="hwA-2">
    <div class="input">
        <label for="hwA-2-serial">#2 - Box Serial Number</label><br />
        <input type="text" name="hwA-2-serial" id="hwA-2-serial" />
    </div>
    <div class="checkbox">
        <label>
            <input type="checkbox" name="hwA-2-override" value=""> Override Validation
        </label>
    </div>
</fieldset>

</div> [...]

I've hacked together something that works flawlessly but it's really ugly code and I want to learn to clean it up.

Does anyone have a better suggestion then this method? I've used some attribute selectors to cut down the repetition, but I still have to a huge block for each hardware (there are 4 right now). I guess I can use a loop with variables for the hardware?

$('input[name="hwA-qty"]').click(function(){
    var selected = $(this).val();
    if (selected == 1) { //Hide inputs 2-5
        $('fieldset[id^="hwA"]').hide(); 
    }
    if (selected == 2) { //Show inputs 1 & 2
        $('fieldset#hwA-2').show();
        $('fieldset#hwA-3').hide();
        $('fieldset#hwA-4').hide();
        $('fieldset#hwA-5').hide();
    }
    if (selected == 3) { //Show inputs 1-3
        $('fieldset#hwA-2').show();
        $('fieldset#hwA-3').show();
        $('fieldset#hwA-4').hide();
        $('fieldset#hwA-5').hide();
    }
    if (selected == 4) { //Show inputs 1-4
        $('fieldset#hwA-2').show();
        $('fieldset#hwA-3').show();
        $('fieldset#hwA-4').show();
        $('fieldset#hwA-5').hide();
    }
    if (selected == 5) { //Show all inputs
        $('fieldset[id^="hwA"]').show();
    }
});
$('input[name="hwB-qty"]').click(function(){
    var selected = $(this).val();
    if (selected == 1) {
        $('fieldset[id^="hwB"]').hide();
    }
    if (selected == 2) {
        $('fieldset#hwB-2').show();
        $('fieldset#hwB-3').hide();
        $('fieldset#hwB-4').hide();
        $('fieldset#hwB-5').hide();
    }
    if (selected == 3) {
        $('fieldset#hdPvr2').show();
        $('fieldset#hdPvr3').show();
        $('fieldset#hdPvr4').hide();
        $('fieldset#hdPvr5').hide();
    }
    if (selected == 4) {
        $('fieldset#hdPvr2').show();
        $('fieldset#hdPvr3').show();
        $('fieldset#hdPvr4').show();
        $('fieldset#hdPvr5').hide();
    }
    if (selected == 5) {
        $('fieldset[id^="sdPvr"]').show();
    }
}); [...]

And here's the html:

[...]
<div class="hardwareValidation">

<div class="select">
    <label for="hwA-qty">Quantity</label><br />
    <label>1 <input type="radio" name="hwA-qty" value="1" checked /></label>
    <label>2 <input type="radio" name="hwA-qty" value="2" /></label>
    <label>3 <input type="radio" name="hwA-qty" value="3" /></label>
    <label>4 <input type="radio" name="hwA-qty" value="4" /></label>
    <label>5 <input type="radio" name="hwA-qty" value="5" /></label>
</div>

<fieldset id="hwA-1">
    <div class="input">
        <label for="hwA-1-serial">#1 - Box Serial Number</label><br />
        <input type="text" name="hwA-1-serial" id="hwA-1-serial" />
    </div>
    <div class="checkbox">
        <label>
            <input type="checkbox" name="hwA-1-override" value=""> Override Validation
        </label>
    </div>
</fieldset>                     

<fieldset id="hwA-2">
    <div class="input">
        <label for="hwA-2-serial">#2 - Box Serial Number</label><br />
        <input type="text" name="hwA-2-serial" id="hwA-2-serial" />
    </div>
    <div class="checkbox">
        <label>
            <input type="checkbox" name="hwA-2-override" value=""> Override Validation
        </label>
    </div>
</fieldset>

</div> [...]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听,心雨的声音 2024-10-29 00:20:26
$('input[name="hwA-qty"]').click(function(){
var selected = $(this).val();

for(i=1;i<=5;i++){
   if(i<=selected)$('fieldset#hwA-'+i).show();
   else $('fieldset#hwA-'+i).hide();
}

}

$('input[name="hwA-qty"]').click(function(){
var selected = $(this).val();

for(i=1;i<=5;i++){
   if(i<=selected)$('fieldset#hwA-'+i).show();
   else $('fieldset#hwA-'+i).hide();
}

}

紫轩蝶泪 2024-10-29 00:20:26

您可以使用 nextAll 遍历方法,该方法选择匹配元素的以下同级元素。

$('input[name="hwA-qty"]').click(function(){
        var selected = $(this).val();
        $("fieldset[id^='hwA-']").show();
        $("#hwA-" + selected).nextAll().hide();
    });

$('input[name="hwA-qty"]:checked').trigger("click"); // Triggers the click event to set to the initial position

请参阅:http://jsfiddle.net/GlauberRocha/s4PAG/1/

You could use the nextAll traversing method, that selects the following siblings of the matched elements.

$('input[name="hwA-qty"]').click(function(){
        var selected = $(this).val();
        $("fieldset[id^='hwA-']").show();
        $("#hwA-" + selected).nextAll().hide();
    });

$('input[name="hwA-qty"]:checked').trigger("click"); // Triggers the click event to set to the initial position

See: http://jsfiddle.net/GlauberRocha/s4PAG/1/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文