选择选项:jQuery、大小写和显示/隐藏表单字段

发布于 2024-12-08 20:00:14 字数 1226 浏览 0 评论 0原文

根据选择控件的值显示/隐藏表单字段的最有效方法是什么?我有一个选择控件和三个输入字段,它们根据您工作的业务类型显示/隐藏。

<select id="business-type">
  <option value="1">Sole Trader</option>
  <option value="2">Partnership</option>
  <option value="3">Public Sector</option>
  <option value="4">Public Limited Company (Plc)</option>
  <option value="5">Charity</option>
</select>

// This should appear if you are a Sole Trader (option 1 in the select box)
<div id="soletrader">
<label for="tax">VAT Number</label>
<input type="text" name="tax" id="tax" /> 
</div>

// This should appear if you are a Public Sector business (option 3 in the select box)
<div id="publicsector">
<label for="urn">URN Number</label>
<input type="text" name="urn" id="urn" />   
</div>

// This should appear if you are a Charity (option 5 in the select box)
<div id="charity">
<label for="charityref">Charity Reference No.</label>
<input type="text" name="charity" id="charityref" />    
</div>

用 jquery 编写的 case 语句是解决这个问题最合适的方法吗?我该如何写这样的声明呢?

默认情况下需要隐藏三个输入字段。任何帮助将不胜感激。我对 javascript/jquery 感到沮丧。

What is the most efficient way of showing/hiding form fields based on the value of a select control? I have one select control and three input fields, which show/hide based upon what type of business you work for.

<select id="business-type">
  <option value="1">Sole Trader</option>
  <option value="2">Partnership</option>
  <option value="3">Public Sector</option>
  <option value="4">Public Limited Company (Plc)</option>
  <option value="5">Charity</option>
</select>

// This should appear if you are a Sole Trader (option 1 in the select box)
<div id="soletrader">
<label for="tax">VAT Number</label>
<input type="text" name="tax" id="tax" /> 
</div>

// This should appear if you are a Public Sector business (option 3 in the select box)
<div id="publicsector">
<label for="urn">URN Number</label>
<input type="text" name="urn" id="urn" />   
</div>

// This should appear if you are a Charity (option 5 in the select box)
<div id="charity">
<label for="charityref">Charity Reference No.</label>
<input type="text" name="charity" id="charityref" />    
</div>

Would a case statement - written in jquery - be the most appropriate approach to this matter? How would I write such a statement?

The three input fields need to be hidden by default. Any assistance would be greatly appreciated. I am dismal at javascript/jquery.

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

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

发布评论

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

评论(5

ゃ懵逼小萝莉 2024-12-15 20:00:14

首先,您应该向每个字段 div 添加“class='field'”。

然后,使用此代码,您将在下拉列表中选择时显示/隐藏适当的字段:

$('#business-type').change(function(){
    var selection = $('#business-type').val();
    $('.field').hide();
    switch(selection){
        case 0:
            $('#soletrader').show();
            break;
        case 1:
            $('#publicsector').show();
            break;
        case 2:
            $('#charity').show();
            break;
    }
});

First, you should add "class='field'" to each field div.

Then, with this code, you will show/hide the appropriate field upon selction in the dropdown:

$('#business-type').change(function(){
    var selection = $('#business-type').val();
    $('.field').hide();
    switch(selection){
        case 0:
            $('#soletrader').show();
            break;
        case 1:
            $('#publicsector').show();
            break;
        case 2:
            $('#charity').show();
            break;
    }
});
生生漫 2024-12-15 20:00:14

有几件事:

1)我会向每个附加字段添加一个类(我在下面使用了 .additional-field)。这将允许您一次性隐藏所有它们,而不是要求您显式地 $.hide() 每一个。只有三个还不错,但如果你最终有更多,它会很快变得毛茸茸的。

2)我将代码放入一个函数中,并在页面加载时调用该函数并将其用作 .change() 的参数。这样可以避免重复相同的代码,并且涵盖了编辑现有值或需要更正表单中的错误而产生的初始值的情况。否则,除非您选择其他内容然后再次选择该项目,否则附加字段不会显示。

$(document).ready(function(){

    function toggleBusinessTypeAdditionalFields() {
        $('.additional-field').hide();

        var selected = $('#business-type').val();
        switch (selected) {
            case 1:
                $('#soletrader').show();
                break;
            case 3:
                $('#publicsector').show();
                break;
            case 5:
                $('#charity').show();
                break;
        }
    }
    toggleBusinessTypeAdditionalFields();
    $('#business-type').change(toggleBusinessTypeAdditionalFields);

});

Few things:

1) I would add a class (I used .additional-field below) to each of the additional fields. This will allow you to hide them all in one swoop instead of requiring you to $.hide() each one explicitly. It's not so bad with just three, but if you ended up with much more, it'd get hairy quick.

2) I put the code in a function and both call that function on page load and use it as the parameter to .change(). This saves repeating the same code, and covers the case of an initial value from either editing an existing one or an error in the form that needs to be corrected. Otherwise, the additional field wouldn't show until you chose something else and then chose the item again.

$(document).ready(function(){

    function toggleBusinessTypeAdditionalFields() {
        $('.additional-field').hide();

        var selected = $('#business-type').val();
        switch (selected) {
            case 1:
                $('#soletrader').show();
                break;
            case 3:
                $('#publicsector').show();
                break;
            case 5:
                $('#charity').show();
                break;
        }
    }
    toggleBusinessTypeAdditionalFields();
    $('#business-type').change(toggleBusinessTypeAdditionalFields);

});
聽兲甴掵 2024-12-15 20:00:14

我认为 if/switch 语句是不必要的。使用更多的 HTML 标记来帮助您的 JavaScript。如果你在每个 div 上放置一个名为“data-val”的属性,并为它分配你希望它一起出现的选项的值,那么 JS 就会变得非常简单。

所以你的 HTML 的 div 看起来像这样:

<div id="soletrader" data-val="1">
    <label for="tax">VAT Number</label>
    <input type="text" name="tax" id="tax" /> 
</div>

然后你的 JS 将是:

 $('select').change(function(){
     var val = $(this).val();
     $('div').hide().filter('[data-val=' + val + ']').show();
 });

I don't think an if/switch statement is necessary. Help your JavaScript out a bit with a little more HTML markup. If you put an attribute named "data-val" on each of the divs, and assign it the value of the option you want it to appear along with, then the JS becomes very simple.

So your HTML would look something like this for the divs:

<div id="soletrader" data-val="1">
    <label for="tax">VAT Number</label>
    <input type="text" name="tax" id="tax" /> 
</div>

And then your JS would be:

 $('select').change(function(){
     var val = $(this).val();
     $('div').hide().filter('[data-val=' + val + ']').show();
 });
扭转时空 2024-12-15 20:00:14

将这些函数放入您的 $.ready(function(){});

$('#soletrader').hide();
$('#publicsector').hide();
$('#charity').hide();

$('#business-type').change(function(){
   var value = $(this).val();
   if(value == '1'){
     $('#soletrader').show();
     $('#publicsector').hide();
     $('#charity').hide();
   } else if(value == '3'){
     $('#soletrader').hide();
     $('#publicsector').show();
     $('#charity').hide();
   } else if(value == '5'){
     $('#soletrader').hide();
     $('#publicsector').hide();
     $('#charity').show();
   } else {
     $('#soletrader').hide();
     $('#publicsector').hide();
     $('#charity').hide();
   }
});

Put these functions in your $.ready(function(){});

$('#soletrader').hide();
$('#publicsector').hide();
$('#charity').hide();

$('#business-type').change(function(){
   var value = $(this).val();
   if(value == '1'){
     $('#soletrader').show();
     $('#publicsector').hide();
     $('#charity').hide();
   } else if(value == '3'){
     $('#soletrader').hide();
     $('#publicsector').show();
     $('#charity').hide();
   } else if(value == '5'){
     $('#soletrader').hide();
     $('#publicsector').hide();
     $('#charity').show();
   } else {
     $('#soletrader').hide();
     $('#publicsector').hide();
     $('#charity').hide();
   }
});
天涯沦落人 2024-12-15 20:00:14
<script type="text/javascript">
               $('#type_of_bussiness').change(function(){
            var selection = $('#type_of_bussiness').val();
       
            if(selection==1)

               {
                  $('#typeofmargin').css('display','block');
               }
               if(selection==2){
                  $('#coloxxcomm').css('display','block');

               }            
                          
             
         });
      </script>
<script type="text/javascript">
               $('#type_of_bussiness').change(function(){
            var selection = $('#type_of_bussiness').val();
       
            if(selection==1)

               {
                  $('#typeofmargin').css('display','block');
               }
               if(selection==2){
                  $('#coloxxcomm').css('display','block');

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