无法在javascript中正确设置值

时间:2015-02-20 16:00:16

标签: javascript jquery

我有6个单选按钮

<input type="text" id="status" name="status">

<input type="radio" name="orderReceivingKeysPresent" id="orderReceivingKeysPresent_Y" value="1" onclick="checkKeyAndTitle(this);"> Yes
<input type="radio" name="orderReceivingKeysPresent" id="orderReceivingKeysPresent_N" value="0" onclick="checkKeyAndTitle(this);"> No

<input type="radio" name="orderReceivingTitlePresent" id="orderReceivingTitlePresent_Y" value="1" onclick="checkKeyAndTitle(this);"> Yes
<input type="radio" name="orderReceivingTitlePresent" id="orderReceivingTitlePresent_N" value="0" onclick="checkKeyAndTitle(this);"> No

<input type="radio" name="orderReceivingReturnToOwner" id="orderReceivingReturnToOwner_Y" value="1" onclick="hideAndShowReturnToOwner(this); checkKeyAndTitle(this);"> Yes
<input type="radio" name="orderReceivingReturnToOwner" id="orderReceivingReturnToOwner_N" value="0" onclick="hideAndShowReturnToOwner(this); checkKeyAndTitle(this);" checked="checked"> No

不,我使用javascript来分配值。我已经创建了javascript函数,但它没有正常工作,我无法弄明白。怎么做?

function checkKeyAndTitle(getObj) {
    if(getObj.name == "orderReceivingKeysPresent") {
        if(getObj.value == "1") {
            $("#status").val('Delivered');
        } else {
            $("#status").val('Missing Keys');
        }
    }
    else {
        if(getObj.value == "1") {
            $("#status").val('Delivered');
        } else {
            $("#status").val('Missing Title');
        }
    }
}

现在orderReceivingReturnToOwner默认选中为否。 当我点击orderReceivingKeysPresent为是和orderReceivingTitlePresent为否时,状态应为Missing Title,当orderReceivingKeysPresent否和orderReceivingTitlePresent是状态应为{{1}时如果两者都是,则状态应为Missing Keys,并在Delivered是状态orderReceivingReturnToOwner时检查此状态。帮助建议我如何做。

1 个答案:

答案 0 :(得分:0)

$("input[type='radio']").on("click", function () {
    var keyRadioVal = $('input:radio[name=orderReceivingKeysPresent]:checked').val();
    var titleRadioVal = $('input:radio[name=orderReceivingTitlePresent]:checked').val();
    var ownerRadioVal = $('input:radio[name=orderReceivingReturnToOwner]:checked').val();

    if (ownerRadioVal == 1) {
        $("#status").val('Return To Owner');
    } else if (keyRadioVal == 1 && titleRadioVal == 0 && ownerRadioVal == 0) {
        $("#status").val('Missing Title');
    } else if (titleRadioVal == 1 && keyRadioVal == 0 && ownerRadioVal == 0) {
        $("#status").val('Missing Keys');
    } else if (titleRadioVal == 1 && keyRadioVal == 1 && ownerRadioVal == 0) {
        $("#status").val('Delivered ');
    } else {
        $("#status").val('Missing Keys');
    }
});

我所做的就是获得每个单选按钮的值,每个单选按钮选择更改时选中,并检查所有3个案例。

在所有情况下休息将显示缺失键,您可以相应地。