2013-01-06 56 views
0

純JavaScript。執行自定義函數onChange

我在HTML頁面有一個複選框。我想執行

App.setCheckedProperty(name, val); 

哪裏name是複選框的name屬性和val真/假來檢查。

如何實現它?我在網上找不到任何材料。

UPD

<input type="checkbox" name="smth" onChange="<WHAT TO DO HERE?>" checked /> 

最終執行必須等於:

App.setCheckedProperty("smth", false); 

UPD2

是否有像在JavaScript this.namethis.checked任何contructions?

+0

它始終是有幫助的,包括一些代碼,你試過未工作。 –

+0

我很抱歉,但我沒有任何代碼 - 我不知道如何去實現它。 –

+0

是的,'this.name'和'this.checked'都存在,並且都應該傳遞給'onchange'中的函數。 –

回答

1

你不應該在你的html中「內聯」。它只是不好的做法。我想你想要做的是循環幾個複選框?

看到這樣的代碼:這裏

// declare all vars that you need and find all input-elements 
var i, input, inputs = document.getElementsByTagName('input'); 

// loop over all input-elements 
for(i = 0; i <= inputs.length; i++) { 
    input = inputs[i]; 

    // if the current element is a checkbox 
    if(input.type === 'checkbox') { 

    //append a click-handler to that checkbox 
    input.onclick = function() { 

     // if the checkbox is clicked, you can find the name and the checked-property 
     App.setCheckedProperty(this.name, this.checked); 
    }; 
    } 
} 

和工作示例(我只是警告,而不是App.setCheckedProperty):http://jsfiddle.net/5wExJ/

相關問題