如何使用Javascript缩短我的代码

时间:2014-05-07 02:36:47

标签: javascript jquery

我的任务就是我所做的但是我想知道选择框是否不仅仅是三个选项...如果它们是5或10个选项,那么让我们想象下面的if else条件如何。 ..

更重要的是,在选择其他选项后,其他选项的input返回为无值作为我的代码。

Demo

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:

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("");
}
});

2 个答案:

答案 0 :(得分:1)

你可以这样做:

$('#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

答案 1 :(得分:0)

您可以使用$("#select_vehicle")的值动态获取要显示的元素的ID。在HTML中,将类select_option添加到选项持有者的所有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:

$("#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');