Вопрос

I have 2 paired input fields, and need to toggle enabled/disabled between them (if one is enabled the other should be disabled).

I have no idea how to approach this, any ideas?

Это было полезно?

Решение

Example: the item1 is enabled, the item2 is disabled

<input type="text" id="item1">
<input type="text" id="item2" disabled>

<button id="switch">Click here to toggle</button>

This will toggle the items when some kind of switch is clicked. Use it on whatever toggles it, i.e. the button above:

$("#switch").on("click", function()
{
    $("#item1").prop("disabled", !$("#item1").prop("disabled"));
    $("#item2").prop("disabled", !$("#item2").prop("disabled"));
});

If you really want to make sure they're opposites;

$("#switch").on("click", function()
{
    var toggle = $("#item1").prop("disabled");

    $("#item1").prop("disabled", !toggle );
    $("#item2").prop("disabled", toggle ); // item2 will always be the opposite of item1
});

Другие советы

I fiddled around the problem and came up with this script:

inputFields = $("#group").find("input");

$("#toggleFields").on("click", function(){
    inputFields.each(toggleElement);        
});

function toggleElement(index, element) {
    element.disabled = !element.disabled;
    //idea from: http://stackoverflow.com/a/4702086/1063730
}
​

​

If you want another event the cause for the toggle you only have to exchange $("#toggleFields").on("click") with your needs.

this gives the enabled input:

<input type="button" value="Button1" />
<input type="button" value="Button2" />
<input type="text" value="Text1" />
<input type="checkbox" value="CheckBox1" />
<input type="radio" value="Radio1" />
<input type="submit" value="Submit1" />
<input type="reset" value="Reset1" />

$(document).ready(function() {
    var id = "";
    // Test
    $('input').attr("disabled", "disabled");
    // Remove attr disabled from text to try code
    $('input[type="text"]').removeAttr("disabled");

    // Check for attr != disabled here
    $('input').each(function () {
        if ($(this).attr("disabled") != "disabled")
        { id = $(this).get(); }
    });
    // Found
    alert($(id).val());
});

http://jsfiddle.net/sVDYe/83/

this toggles the disable for all inputs but checkbox:

<input type="button" value="Button1" disabled="disabled" />
<input type="button" value="Button2" disabled="disabled" />
<input type="text" value="Text1" disabled="disabled" />
<input type="checkbox" value="CheckBox1" />
<input type="radio" value="Radio1" disabled="disabled" />
<input type="submit" value="Submit1" disabled="disabled" />
<input type="reset" value="Reset1" disabled="disabled" />

$(document).ready(function() {
    var id = "";
    // Check for attr != disabled here
    $('input').each(function () {
        if ($(this).attr("disabled") != "disabled")
        { id = $(this).get(); }
    });
    // Found
    alert($(id).val());
    // Make sure all disabled but this
    $('input').attr("disabled", "disabled");
    $(id).removeAttr("disabled");
});

http://jsfiddle.net/sVDYe/88/

and at last toggle between two input:

$(document).ready(function() {
    $('input').click(function () {
        var id = $(this).get();
        toggleButtons(id);
    });
    function toggleButtons(id){
        // Check for attr != disabled here
        $('input').removeAttr("disabled");
        $(id).attr("disabled","disabled");
    }        
});

http://jsfiddle.net/sVDYe/91/

Simple as this. also makes sure that only the true one enabled.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top