2012-10-28 83 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這樣的單選按鈕的名稱。 我得到了一個要求,使用for循環來增加單選按鈕q0,q1,q2等的名稱。幫我出去..在Javascript中使用變量

+1

這是無關JSP,但它關係到的JavaScript。請適當標記。 –

+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來展示爲每個單選按鈕和chan分配相同名稱的區別使每個單選按鈕的名稱不同。

第一行中的單選按鈕全部都是可檢查的......但是您將無法取消選中它們,因爲它們沒有任何關閉它們的功能(沒有其他單選按鈕具有相同的名稱可將它們關閉)。

第二行中的單選按鈕將以您希望單選按鈕組工作的方式工作......每次只能選中其中一個單選按鈕。

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> 

的Javascript:

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(); 
}();