2012-10-28 49 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(); 
}();