简体   繁体   English

如何使用JavaScript缩短我的代码

[英]How to shorten my code with Javascript

My task is what I have done but I am wondering about if the select box is not only three options... If they are 5 or 10 options so let's imagine that how the if else condition below can be... 我的任务是完成任务,但我想知道选择框是否仅是三个选项...如果它们是5或10个选项,那么让我们想象一下下面的if else条件如何可以...

More than that, after choosing another option, the others option's input will be returned to no value as my code. 不仅如此,在选择另一个选项后,其他选项的input将作为我的代码返回没有任何值

Demo 演示

HTML: HTML:

<table>
<tr><td>123</td></tr>
<tr><td>456</td></tr>
<tr><td>...</td></tr>
<tr>
    <td>
        <select id="select_vehicle">
            <option value="company_vehicle">Company Vehicle</option>
            <option value="hiring_vehicle">Hiring Vehicle</option>
            <option value="taxi">Taxi</option>
        </select>
    </td>
</tr>
<tr id="company_vehicle">
    <td>Company Vehicle</td>
    <td>
        <input type="text" />
    </td>
    <td>
        <input type="radio" name="vehicle" value="bus" />Bus</td>
    <td>
        <input type="radio" name="vehicle" value="train" />Trolley Car</td>
</tr>
<tr id="hiring_vehicle">
    <td>Hiring Vehicle</td>
    <td>
        <input type="radio" name="vehicle" value="bus" />Bus</td>
    <td>
        <input type="radio" name="vehicle" value="train" />Trolley Car</td>
    <td>
        <input type="text" />
    </td>
</tr>
<tr id="taxi">
    <td>Taxi</td>
    <td>
        <input type="checkbox" name="vehicle" value="Taxi" />Taxi
    </td>
</tr>
<tr><td>...</td></tr>
<tr><td>123</td></tr>
<tr><td>456</td></tr>
</table>

Javascript: 使用Javascript:

var select_vehicle = $("#select_vehicle");
// Set selected item
var set_selected_item = $(select_vehicle).val("company_vehicle");
// Hide options "Hiring Vehicle" & "Taxi"
var company_vehicle = $("#company_vehicle"); // Get id "Company Vehicle"
var hiring_vehicle = $("#hiring_vehicle"); // Get id "Hiring Vehicle"
hiring_vehicle.hide();
var taxi = $("#taxi"); // Get id "Taxi"
taxi.hide();

$(select_vehicle).on('change', function (e) {
var optionSelected = $("option:selected", this);
var valueSelected = this.value;
if (valueSelected == "company_vehicle") {
    company_vehicle.show();
    hiring_vehicle.hide();
    taxi.hide();
    $("#taxi input").removeAttr('checked');
    $("#hiring_vehicle input").removeAttr('checked').val("");
} else if (valueSelected == "hiring_vehicle") {
    company_vehicle.hide();
    hiring_vehicle.show();
    taxi.hide();
    $("#company_vehicle input").removeAttr('checked').val("");
    $("#taxi input").removeAttr('checked');
} else {
    company_vehicle.hide();
    hiring_vehicle.hide();
    taxi.show();
    $("#company_vehicle input").removeAttr('checked').val("");
    $("#hiring_vehicle input").removeAttr('checked').val("");
}
});

You can do: 你可以做:

$('#select_vehicle').change(function() {
    var val = this.value;
    $('#'+val).find('input[type="text"]').val('');
    $('#'+val).find('input[type="radio"]').prop('checked',false);
    $('#'+val).show().siblings('tr[id]').hide();    
}).change();

Updated Fiddle 更新小提琴

You can use the value of $("#select_vehicle") to dynamically get the id of the element to show. 您可以使用$("#select_vehicle")的值动态获取要显示的元素的ID。 In the HTML add the class select_option to all of the tr 's that are option holders like: 在HTML中,将class select_optionselect_option到所有tr ,这些tr都是选项持有者,例如:

<tr id="company_vehicle" class="select_option">
    <td>Company Vehicle</td>
    <td>
        <input type="text" />
    </td>
    <td>
        <input type="radio" name="vehicle" value="bus" />Bus</td>
    <td>
        <input type="radio" name="vehicle" value="train" />Trolley Car</td>
</tr>
<tr id="hiring_vehicle" class="select_option">
    <td>Hiring Vehicle</td>
    <td>
        <input type="radio" name="vehicle" value="bus" />Bus</td>
    <td>
        <input type="radio" name="vehicle" value="train" />Trolley Car</td>
    <td>
        <input type="text" />
    </td>
</tr>
<tr id="taxi" class="select_option">
    <td>Taxi</td>
    <td>
        <input type="checkbox" name="vehicle" value="Taxi" />Taxi</td>
</tr>

Javscipt: Javscipt:

$("#select_vehicle").on('change', function(){
    var optionSelected = $("option:selected", this);
    var valueSelected = this.value;

    $('.select_option').hide();
    $('.select_option input[type="text"]').val('');
    $('.select_option input:checked').prop('checked', false);

    $('#'+valueSelected).show();
}).trigger('change');

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

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