2016-10-10 113 views
0

我創建一個在線考試門戶以下是顯示問題,考生的代碼。我不知道如何處理由考生如何檢查哪個單選按鈕(動態)被檢查?

$("input[name='hello']").click(function(){ 
    alert("you clicked on"); 
}); 

單選按鈕是動態的100個問題4各

+2

你這是什麼意思是__handle__? – Satpal

+0

只是我每個顯示100個問題4個選項。並讓考生回答這些問題。在這裏,我試圖評估候選人天氣的右邊錯誤所檢查的問題,並最終顯示得分。 –

+1

SO – Satpal

回答

0

創建點擊答案考慮一個值爲每個選項

然後用它來了解其中一個被點擊:

$("input[name='hello']:checked").val(); 

你可以重寫你這樣的代碼:

$("input[name='hello']").click(function(){ 
    var v=$("input[name='hello']:checked").val(); 
    alert("you clicked on"+v); 
}); 
0

假設你有沒有和身邊每4個箱questionWrapper類股利你可以做這樣的事情:

var collection = $(".questionWrapper"); 
$.each(collection, function(i, $questionWrapper, function(){ 
    //Do this for all the questions 
    //Find the checked input (maybe use classes here...) and get its value 
    var clickedAnswer = $($questionWrapper).find("input:checked").val(); 
    //Do something with the clicked var, maybe push it to an array and then compare the array to the solution 
}); 
2

試一試 -

$('input[data-type=choice]').change(function() { 
 
    var Question = $(this).attr('name'); 
 
    var Checked = $(this).attr('value'); 
 
    console.log('Selected Choice for ' + Question + ' is ' + Checked); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="button" value="Log all Answers" onclick="logAllAnswers()"> 
 
<input type="button" value="Clear Log" onclick="console.clear();"> 
 
<hr> 
 
<form> 
 
    <fieldset> 
 
    <legend>1. Select the answer for the first question.</legend> 
 
    <input type="radio" data-type="choice" name="Q1" value="1">Option 1 
 
    <br> 
 
    <input type="radio" data-type="choice" name="Q1" value="2">Option 2 
 
    <br> 
 
    <input type="radio" data-type="choice" name="Q1" value="3">Option 3 
 
    <br> 
 
    <input type="radio" data-type="choice" name="Q1" value="4">Option 4 
 
    </fieldset> 
 
    <fieldset> 
 
    <legend>2. Select the answer for the second question.</legend> 
 
    <input type="radio" data-type="choice" name="Q2" value="1">Option 1 
 
    <br> 
 
    <input type="radio" data-type="choice" name="Q2" value="2">Option 2 
 
    <br> 
 
    <input type="radio" data-type="choice" name="Q2" value="3">Option 3 
 
    <br> 
 
    <input type="radio" data-type="choice" name="Q2" value="4">Option 4 
 
    </fieldset> 
 
    <fieldset> 
 
    <legend>3. Select the answer for the third question.</legend> 
 
    <input type="radio" data-type="choice" name="Q3" value="1">Option 1 
 
    <br> 
 
    <input type="radio" data-type="choice" name="Q3" value="2">Option 2 
 
    <br> 
 
    <input type="radio" data-type="choice" name="Q3" value="3">Option 3 
 
    <br> 
 
    <input type="radio" data-type="choice" name="Q3" value="4">Option 4 
 
    </fieldset> 
 
</form> 
 

 

 

 
<script> 
 
    function logAllAnswers() { 
 
    $('input[data-type=choice]:checked').each(function() { 
 
     var Question = $(this).attr('name'); 
 
     var Checked = $(this).attr('value'); 
 
     console.log('Selected Choice for ' + Question + ' is ' + Checked); 
 
    }); 
 
    } 
 
</script>

+0

data-type =「choise」不支持 –

+0

獲得選定的單選按鈕值你的意思是不支持。你使用jQuery嗎? - 檢查結果中的代碼段的作品。 –

+0

在我的編輯記事本+ +的數據類型=「的choise」在黑色顯示屬性,我認爲它不支持。 雅其片段,但本地主機它不是迴環 –