2010-03-17 1 views
0

양식이있는 html 페이지가 있습니다.
양식은 텍스트 상자, 라디오, 체크 박스 등과 같은 입력 요소와 동적으로 채워 도착하는 사업부를 가지고동적으로 생성 된 요소의 Jquery 값 검색

지금은 수 있도록, HTML 페이지에서 이러한 동적으로 생성 된 요소의 값을 검색 원하는 내가 그것을 페이지에 제출할 수 있습니다.

// HTML 페이지

<script type="text/javascript"> 
     $(function() { 
      populateQuestions(); 
     });  

     $("#submit_btn").click(function() { 
      // validate and process form here 
      //HOW TO ??retrieve values??? 
      var optionSelected = $("input#OptionSelected_1").val();// doesn't work? 

      // alert(optionSelected); 
      postAnswer(qid,values);//submit values 
      showNextQuestion() ;// populate Div Questions again new values 
     }); 


    </script> 

    <form action="" name="frmQuestion"> 
    <div id="Questions" style="color: #FF0000"> 

    </div> 

// 질문 DIV 세대 스크립트 예를 들어 라디오 버튼

//questionText text of question 
//option for question questionOptions 
// **sample call** 
var question = createQuestionElement("1","MCQ", "WHAT IS ABCD??", "Opt1$Opt2$Opt3"); 
question.appendTo($('#Questions')); 


function createQuestionElement(id, type, questionText, questionOptions) { 
    var questionDiv = $('<div>').attr('id', 'Question'); 
    var divTitle = $('<div>').attr('id', 'Question Title').html(questionText); 
    divTitle.appendTo(questionDiv);  
    var divOptions = $('<div>').attr('id', 'Question Options');  
    createOptions(id, "radio", questionOptions, divOptions); 
    divOptions.appendTo(questionDiv); 
    return questionDiv; 
} 

function createOptions(id, type, options, div) { 
    var optionArray = options.split("$"); 
    // Loop over each value in the array. 
    $.each(
optionArray, function(intIndex, objValue) { 
    if (intIndex == 0) { 
     div.append($("<input type='" + type + "' name='OptionSelected_" + id + "' checked='checked' value='" + objValue + "'>")); 
    } else { 
     div.append($("<input type='" + type + "' name='OptionSelected_" + id + "' value='" + objValue + "'>")); 
    } 
    div.append(objValue); 
    div.append("<br/>"); 
} 

답변

1

당신이 분할하고 있기 때문에 당신은 동일한 입력 여러 번 작성 옵션 배열은 다음과 같이 만듭니다.

<input type='radio' name='OptionSelected_1' checked='checked' value='Opt1'> 
<input type='radio' name='OptionSelected_1' value='Opt2'> 
<input type='radio' name='OptionSelected_1' value='Opt3'> 

당신은 대신이처럼 :checked 항목을 name으로 가져오고 싶지는 #와 ID에 의해 인출됩니다

var optionSelected = $("input[name=OptionSelected_1]:checked").val(); 
+1

Amitd - 내가 코드를 다시 읽어은 ... 업데이트 된 대답을 시도 , 당신이 뭘했는지,하지 않으면 의견을 주시기 바랍니다 –

+0

멋진이 작품. Thx 많이 :) – Amitd

+0

지금 완료되었습니다. 코드를 테스트했습니다. 은 여러 개의 라디오 버튼 그룹 또는 질문 인 경우 질문을 할 수 있습니다.이 값을 선택하는 방법은? 양식의 입력 요소 수를 계산하는 방법 ?? – Amitd