2

我的表单有 2 个单选按钮(是和否)和一个文本框。如果用户单击是,它会启用文本框,您可以输入信息并将其上传到数据库,包括单选按钮中的值。如果单击否,它将禁用文本框并假设仅将单选框的值上传到数据库。但我不明白。

<input type="radio" name="TermLease" value="No" onclick="TermLeaseMonths.disabled=true">No
<input type="radio" name="TermLease" value="Yes"  onclick="TermLeaseMonths.disabled=false">Yes | 
How many months:<input type="hidden" name="TermLeaseMonths"  value="0" />
<input type="text" name="TermLeaseMonths" id="TermLeaseMonths" size="1" disabled="true">

我有一个隐藏的输入类型,可以上传值。但是当我单击是时,它不会禁用文本框。不知道我哪里出错了。

4

3 回答 3

0

点击单选按钮试试这个,我们可以这样做

$('#radio').click(function () {
    if (("#radio").val() == "") {
        $('class_name_oftextBox').attr("disabled", true);
    } else {
        $('class_name_oftextBox').removeAttr("disabled");
    }
});

或者你可以这样做

$('#enable').click(function () {
    $('#textBox').removeAttr("disabled")
});
$('#disable').click(function () {
    $('#textBox').attr("disabled", "disabled");
});
于 2013-09-02T06:14:38.537 回答
0

演示 jsFiddle

HTML

<form>
    <span style="float: left;">
        <label><input type="radio" name="TermLease" value="No" onclick="ShowHideTextbox('leaseMonths', false)" />No</label>
        <label><input type="radio" name="TermLease" value="Yes"  onclick="ShowHideTextbox('leaseMonths', true)"/>Yes</label>
    </span>

    <span id="leaseMonths" style="display: none; float: left;">
        <span> | </span>  <span>How many months: </span>
        <input type="text" name="TermLeaseMonths" id="TermLeaseMonths" size="1" />
    </span>
</form>

JS

var previousNumberOfMonths = "0";

function ShowHideTextbox(elementId, show) {
    var link = document.getElementById(elementId);
    var textbox = document.getElementById("TermLeaseMonths");
    if (show){    
        textbox.value = previousNumberOfMonths;

        link.style.display = 'block';
    }
    else {
        link.style.display = 'none';
        previousNumberOfMonths = textbox.value;

        document.getElementById("TermLeaseMonths").value = "0";
    }
}
于 2013-09-03T04:58:59.110 回答
0

您忘记为文本字段提供 id。尝试这个

<input type="radio" name="TermLease" value="No" onclick="TermLeaseMonths.disabled=true">No
<input type="radio" name="TermLease" value="Yes"  onclick="TermLeaseMonths.disabled=false">Yes | 
How many months:<input type="hidden" name="TermLeaseMonths"  value="0" />
<input type="text" name="TermLeaseMonths" id="TermLeaseMonths" size="1" disabled="true">
于 2013-09-02T04:53:59.793 回答