2012-10-28 2 views
0
<head> 
    <script type="text/javascript"> 
     var p=0; 
    </script> 
</head> 
<body> 
<form name="quiz" method="post" action="evaluate.jsp"> 
    <script type="text/javascript"> 
        <input type="radio" name="q"+p value="hi">charles 
        <input type="submit"> 
    </script> 

    </form> 
    </body>  

누구나 내가 q0과 같은 radiobutton의 이름을 연결하는 방법을 알려줄 수 있습니까? for 루프를 사용하여 radiobutton q0, q1, q2 등의 이름을 증가시키는 요구 사항이 있습니다. 도와주세요 ..자바에서 변수 사용

+1

이 JSP와 관련이 있지만,이 자바 스크립트 관련이있다. 적절하게 태그하십시오. –

+2

'

0

(경우에 따라서는 브라우저에 의해 일부 페이지로드 최적화를 방지 할 수 있습니다) document.write()하는 대신, 그냥 부하 후 DOM을 수정하고 입력 태그에 원하는 name 속성을 추가하는 스크립트를 사용하는 것입니다 .

<head> 
<script type="text/javascript"> 
    var p=0; 
</script> 
</head> 
<body> 

<form name="quiz" method="post" action="evaluate.jsp"> 
    <input id="radio1" type="radio" value="hi">charles 
    <input type="submit"> 
</form> 
<script type="text/javascript"> 
    document.getElementById("radio1").name = "q" + p; 
</script> 
</body>  
0

이것은 당신의 입력을 생성하는 스크립트 태그 안에 가야한다 :

function createInputs(totalInputsNeeded) { 
    var rdo, parent; 

    parent = document.getElementById('parentId'); 

    for (var i = 0; i < totalInputsNeeded; i++) {   
     rdo = document.createElement('input'); 

     rdo.setAttribute('type','radio'); 
     rdo.setAttribute('name','q' + i.toString()); 
     rdo.setAttribute('value', 'yourValue'); 

     parent.appendChild(rdo); 
    } 
} 

를 그냥 기존 라디오의 이름을 추가 할 수 있다면, 당신은이 작업을 수행 할 수 있습니다

function appendToNames() { 
    var elems = document.getElementsByName('q'); 

    for (var i = 0; i < elems.length; i++) { 
     elems[i].setAttribute('name', elems[i].getAttribute('name') + i.toString()); 
    } 
} 
0

OP가 요구 한대로 가능한 문제를 설명하는 것이 중요하다고 생각합니다. 그래서 jsFiddle을 구성하여 모든 라디오 버튼과 채널에 동일한 이름을 할당하는 것의 차이점을 보여주었습니다 각 라디오 버튼의 이름을 다르게 지정하십시오.

첫 번째 행의 라디오 버튼은 모두 선택 가능하지만 선택 해제 할 수 없으므로 선택을 취소 할 수 없습니다 (동일한 라디오 버튼을 사용하여 다른 라디오 버튼을 껐다 켤 수 없음).).

두 번째 행의 라디오 버튼은 라디오 버튼 그룹이 작동하는 방식으로 작동합니다 ... 한 번에 하나의 라디오 버튼 만 선택 가능합니다.

http://jsfiddle.net/2ENZP/1/

HTML :

<form name="quiz" method="post" action="evaluate.jsp"> 
    <h3>Buttons with Differing Names</h3> 
    <fieldset id="buttonsDiffNames"> 
    </fieldset> 

    <h3>Buttons with Same Names</h3> 
    <fieldset id="buttonsSameNames"> 
    </fieldset> 
    <input type="submit"> 
</form> 

자바 스크립트 :

var radioBtns = [ 
    {id:0,name:"charles"}, 
    {id:1,name:"diana"}, 
    {id:2,name:"peter"}, 
    {id:3,name:"sofia"}, 
    {id:4,name:"reggie"}, 
    {id:5,name:"jim"} 
]; 

// set up templates for use by the examples, below. 
var tmpl = '<input type="radio" name="{ID}" value="{ID}">{NAME}'; 
var tmpl2 = '<input type="radio" name="{ID}" value="{NAME}">{NAME}'; 

// This one will populate the first list, with buttons 
// that all have different names 
function populateWithDifferingNames(){ 
    // get a ref to the fieldset we're going to add these to 
    var fieldSet = document.getElementById("buttonsDiffNames"); 
    // create an array to act as a StringBuilder 
    var sb = []; 

    // loop through your dataset... 
    for(var i = 0; i < radioBtns.length; i++){ 
     // add a string to the sb array, replacing the templated 
     // areas with the incremented radio button id and the name 
     // from the dataset 
     sb.push(tmpl.replace(/\{ID\}/gi,"q" + radioBtns[i].id).replace("{NAME}", radioBtns[i].name)); 
    } 
    // set the fieldset's innerHTML to the joined string representation 
    // of the sb array 
    fieldSet.innerHTML = sb.join("\n"); 
} 

// Same idea, but with matching names 
function populateWithSameNames(){ 
    var fieldSet = document.getElementById("buttonsSameNames"); 
    var sb = []; 

    for(var i = 0; i < radioBtns.length; i++){ 
     sb.push(tmpl2.replace(/\{ID\}/gi,"q").replace(/\{NAME\}/gi, radioBtns[i].name)); 
    } 
    fieldSet.innerHTML = sb.join("\n"); 
} 

window.onload = function() { 
    populateWithDifferingNames(); 
    populateWithSameNames(); 
}();