簡體   English   中英

Javascript / jQuery:從表單獲取特定元素

[英]Javascript/jQuery: Get specific elements from form

我有一個約有25個輸入(文本,單選框和復選框)的擴展表單。 我希望當我單擊打開jQuery對話框的按鈕時,加載表單並設置除5個禁用的所有字段。 似乎很簡單,但我希望將其轉換為“通用”功能。 我的意思是,我有這種方法:

function disableInputs(jQueryElement, exceptions, booleanClean) {
    //Some stuff
}

我想從jQueryElement獲取所有輸入,但忽略具有exceptions的id的所有元素。 Exceptions是一個像這樣的對象:

var exceptions = {
    0: 'clientId',
    1: 'clientName',
    2: 'clientFirstSurname',
    3: 'clientSecondSurname',
    4: 'clientAlias'
}

這是我的完整代碼,也是我測試過的代碼,但這是使其工作的唯一方法,如果我收到了第三個參數( booleanClean ),它將為所有inputs設置value='' ,而不是元素沒有被排除在殘疾人之外。 該布爾值用於檢查在調用此函數時是否要清除輸入:

function disableInputs(jQueryElement, exceptions, booleanClean) {
    var inputs = jQueryElement.find('input');
    for (var i = 0; i < inputs.length; i++) {
        inputs[i].setAttribute('disabled', true);
        for (var attr in exceptions) {
            if (inputs[i].getAttribute('id') === exceptions[attr]) {
                inputs[i].removeAttribute('disabled');
            } else {
                if (booleanClean === true) {
                    inputs[i].value = null;
                }
            }
        }
    }
}

我知道為什么不能使用干凈的“選項”。 我想要的是我必須將其正確放置的位置,或者如果我可以在獲取輸入時設置條件以僅獲取未排除的輸入(優選的第二個優化選項,而不是為每個輸入設置屬性,並且如果已排除,則將其刪除。似乎更容易工作)。

我建議將exceptions對象更改為常規數組:

var exceptions = ['clientId',
                  'clientName',
                  'clientFirstSurname',
                  'clientSecondSurname',
                  'clientAlias'];

...因為這樣可以大大簡化您的功能

function disableInputs(jQueryElement, exceptions, booleanClean) {
    var inputs = jQueryElement.find('input');
    if (exceptions.length > 0) {
        exceptions = "#" + exceptions.join(",#");
        inputs = inputs.not(exceptions);
    }
    inputs.prop("disabled",true);
    if (booleanClean)
        inputs.val("");
}

對於您是要清除所有輸入還是僅清除不在例外列表中的輸入,我有些困惑。 我上面的代碼只是清除那些不在列表中的代碼。 要清理它們,請移動if(booleanClean) inputs.val(""); 到另一個if語句之前。

嘗試

function disableInputs(jQueryElement, exceptions, booleanClean) {
    var not = jQuery.map(exceptions, function(item, index){
        return '#' + item;
    }).join(',')
    var inputs = jQueryElement.find(':input').not(not).prop('disabled', true);
    if(booleanClean){
        inputs.val('')
    }
}

您是否可以為例外項指定類名? 那就是我會做的。

<input class="exception" />

$( "input:not(.exception)" ).prop("disabled", true);

試試這個:

的HTML

<form>
    <input type="text" name="input1" value="val1" />
    <input type="text" name="input2" value="val2" />
    <input type="text" name="input3" value="val3" />
</form>

JS

function disableInputs(jQueryElement, exceptions, booleanClean) {

    jQueryElement.find('input').not( exceptions.join(', ') ).each(function(){

        if( booleanClean ){
            $(this).val('');
        }

        $(this).prop('disabled', true);

    });

}


var exceptions = ['input[name=input1]', 'input[name=input3]'];

disableInputs( $('form'), exceptions, true );

這是工作示例: http : //jsfiddle.net/4Dwwk/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM