Getting array of Values of Textboxes with the same class
I setup custom CSS class for array of dynamic TextBoxes (inputs as HTML)
so... now I need to get array of it :
<input type="text" style="width: 50px;" class="DynamicTB" id="ctl00_ContentPlaceHolder1_GridView1_ctl02_id" readonly="readonly" value="1" name="ctl00$ContentPlaceHolder1$GridView1$ctl02$id">
sure client don't really knows the count of inputs. That's why I use class and here is what I'm trying to make :
$.each( { id : $("input.DynamicTB").css("value") },
function(id){
CallPageMethod("SelectBook", success, fail, "id",id);
});
I'm not sure if this $("input.DynamicTB").css("value") will works correct :( but
How can I transfer whole array of values to SelectBook Me开发者_运维百科thod ?
My javascript is bad and my debugger don't show me javascript errors but it just doesn't works because of something wrong with each.
And ... finally I just need to get array of values of dynamic textboxes and transfer them to server side ... mt [WebMethode] can't see server side :(
[Web.Services.WebMethod]
public static SelectBook(id : array) : string
{
id
}
And sure I have no idea how can I use jQuery .live and binding here.
ok... now problems solved , here is final way I do it XD
Page :
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
function CallPageMethod(methodName, onSuccess, onFail)
{
var args = '';
var l = arguments.length;
if (l > 3)
{
for (var i = 3; i < l - 1; i += 2)
{
if (args.length != 0) args += ',';
args += '"' + arguments[i] + '":"' + arguments[i + 1] + '"';
}
}
var loc = window.location.href;
loc = (loc.substr(loc.length - 1, 1) == "/") ? loc + "Report.aspx" : loc;
$.ajax
({
type: "POST",
url: loc + "/" + methodName,
data: "{" + args + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: onSuccess,
fail: onFail
});
}
function select() {
var values = new Array();
$("input.DynamicTB").each
(
function(i) {
values[i] = $(this).val();
}
);
var valuesG = new Array();
$("input.DynamicTBG").each
(
function(i) {
valuesG[i] = $(this).val();
}
);
CallPageMethod("SelectBook", success, fail, "values", values, "valuesG", valuesG);
}
function success(response)
{
alert(response.d);
}
function fail(response)
{
alert("Ошибка.");
}
</script>
Adding Code :
unless (GridView1.Rows.Count==0)
{
mutable ts=GridView1.Rows[0].Cells[0].Text.Split(':');
mutable first=true;
mutable cntrl=ts[1];
foreach(index with row = GridView1.Rows[index] in [0..GridView1.Rows.Count-1])
{
unless (first)
ts=row.Cells[0].Text.Split(':');
unless (ts.Length==1)
{
if (ts[1]==cntrl && !first)
{
row.Cells[0].Text=ts[0];
}
else
{
row.Cells[0].Text="";
row.Cells[0].Controls.Add
({
def TB = TextBox(); TB.EnableViewState = false;
unless(row.Cells[0].Text == " ")
{
TB.Text = ts[0];
}
TB.Visible=true;
TB.ID="id"+TB.ClientID;
TB.Width = 50;
TB.CssClass="DynamicTB";
TB
});
row.Cells[0].Controls.Add
({
def TBQ = TextBox(); TBQ.EnableViewState = false;
unless(row.Cells[0].Text == " ")
{
TBQ.Text = cntrl;
}
TBQ.Width = 50;
TBQ.Visible=true;
TBQ.CssClass="DynamicTBG";
TBQ
});
}
when(first) first=false;
cntrl=ts[1];
}
}
}
Event Code :
[Web.Services.WebMethod]
public static SelectBook(values : string, valuesG : string) : string
{
def vals = values.Split(',');
def valsG = valuesG.Split(',');
SQLModule.UpdateAdvCode(vals, valsG)
}
SQL Code :
public static UpdateAdvCode(vals : array[string], valsG : array[string]) : string
{
conn.Open();
foreach(i in [0..vals.Length-1])
unless (vals[i]=="" || vals[i]==" ")
{
def cmd = SqlCommand("UPDATE CfgGIS SET AdvCode='"+vals[i]+"' WHERE ID_GIS="+valsG[i], conn);
try
{
_=cmd.ExecuteNonQuery();
}
catch
{
| e is Exception => return e.ToString();
}
}
conn.Close();
"Updated"
}
and yeah ... I still need to fix adding code LOL
by the way : The book is a lie and there is no book :P just save it's name from original manual
var values = [];
$("input.DynamicTB").each(function(){
values.push( $(this).val() ); // this is the value of each textbox
})
values is now an array with all the values of your textboxes...
精彩评论