简体   繁体   English

如何使用相同的javascript控制表单中的不同元素

[英]How to control different elements in the form using same javascript

I have more than one rows in my form. 我的表格中有多行。 Basically two dropdown boxes in each row. 基本上每行有两个下拉框。 Value of second dropdown box gets updated based on selection in the first drop down box. 第二个下拉框的值将根据第一个下拉框中的选择进行更新。 How do I apply javascript to individual rows at a time? 如何一次将JavaScript应用于单个行? Say when I change first dropdown box in second row, i want second dropdown to get updated only in the second row. 说,当我更改第二行中的第一个下拉框时,我希望第二个下拉列表仅在第二行中得到更新。 Also, how do I use arrays in to update multiple rows data to the same database. 另外,如何使用数组将多个行数据更新到同一数据库。 Here is my code: 这是我的代码:

<form action='purchase.php' method="POST"> 
<table><tr><td>Supplier</td><td>Item Name</td></tr>
<tr><td><select name ="supplier" id = "supplier" onchange = "updatesup()">
<option value = "1"> SUPA </option><option value = '2'>SUPB</option></select> </td>
<td> <Select name ="itemname" id="itemname"><option value = ""> Select Item</option></select>/td></tr>
<tr><td><select name ="supplier" id = "supplier" onchange = "updatesup()">
<option value = "1"> SUPA </option><option value = '2'>SUPB</option></select> </td>
<td> <Select name ="itemname" id="itemname"><option value = ""> Select Item</option></select>/td></tr></table></form>

on selecting a supplier..i want to change the items in the itemname dropdown list..but i want the change to happen to respective dropdown lists only...how do i do this? 在选择供应商时..我想更改项目名称下拉列表中的项目。.但我希望仅对相应的下拉列表进行更改...我该怎么做?

this is the javascript I am using 这是我正在使用的javascript

array iteam_a;
item_a[0] = [1, item1, 1];
item_a[1] = [2, item2, 1];
item_a[2] = [3, item2, 1];
item_a[3] = [4, item4, 2];
item_a[4] = [5, item5, 2];
item_a[5] = [6, item6, 2];
function updatesup(){
    removeAllOptions(document.getElementById('itemname'));
    addOption(document.getElementById('itemname'), "", "Select Item");

    for (var i = 0; i <= item_a.length-1; i++){
        if (item_a[i][2] === document.getElementById('supplier').value){
                addOption(document.getElementById('itemname'), item_a[i][0], item_a[i][1]);
            }
     }   

 }

function addOption(selectbox, value, text ){
    var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;

selectbox.options.add(optn);
 }               

function removeAllOptions(selectbox)
{
var j;
for(j=selectbox.options.length-1;j>=0;j--)
{
    selectbox.remove(j);
}
   }      

Have a look at knockout.js 看看基因敲除.js

It allows you to build a MVVM model in Javascript, and what you want can easy be achieved. 它允许您使用Javascript构建MVVM模型,并且可以轻松实现所需的功能。 On the site there are a lot of examples. 该站点上有很多示例。 And on Channel 9 there is a nice presentation of knockout.js from Steve Sanderson. 第9频道上,有史蒂夫·桑德森(Steve Sanderson)的knockout.js的精彩演示。

You may change the signature of updatesetup method. 您可以更改updatesetup方法的签名。

Define two parameters, lets say mainindex and subindex, that must receive the index of each element from that row, something like: 定义两个参数,比如说mainindex和subindex,它们必须从该行接收每个元素的索引,例如:

function updatesup(mainindex, subindex){
    removeAllOptions(document.getElementById('itemname_' + subindex));
    addOption(document.getElementById('itemname_' + subindex), "", "Select Item");

    for (var i = 0; i <= item_a.length-1; i++){
        if (item_a[i][2] === document.getElementById('suplier_' + subindex).value){
                addOption(document.getElementById('itemname_' + subindex), item_a[i][0], item_a[i][1]);
        }
 }   

Note that I have changed the ids to be searched ... so, in yout html built th rows with an index sequence 请注意,我已经更改了要搜索的ID ...因此,在html中,使用索引序列构建了行

<form action='purchase.php' method="POST"> 
<table><tr><td>Supplier</td><td>Item Name</td></tr>
<tr><td><select name ="supplier_0" id = "supplier_0" onchange = "updatesup()">
<option value = "1"> SUPA </option><option value = '2'>SUPB</option></select> </td>
<td> <Select name ="itemname_0" id="itemname_0"><option value = ""> Select Item</option>        </select>/td></tr>
<tr><td><select name ="supplier_1" id = "supplier_1" onchange = "updatesup()">
<option value = "1"> SUPA </option><option value = '2'>SUPB</option></select> </td>
<td> <Select name ="itemname_1" id="itemname_1"><option value = ""> Select Item</option>        </select>/td></tr></table></form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM