夥計們我有兩頁GridviewWithJquery.aspx和Ekle.aspx。如何獲取aspx頁面上的文本框值加載
在當我點擊Ekle按鈕,第一個文件,我加載Ekle.aspx入格(與id="content"
)。
之後,我嘗試獲取位於Ekle.aspx
中的文本框值。
我無法使用正常語法($("#ekle_gonder").click(function){...};)
獲取該值,並且沒有進入點擊功能也沒有獲得文本框值。
如何獲取該值?
下面是我的代碼:
GridviewWithJquery.aspx:
<script>
$(document).ready(function() {
var textregex = /^[A-Za-z çğıöşü]{1,25}$/;
var name = "";
printValues();
function printValues() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "GridviewWithJquery.aspx/fnc",
data: "{}",
dataType: "json",
success: function (result) {
$("#gw1").find("td").remove();
for (var i = 0; i < result.d.length; i++) {
$("#gw1").append("<tr><td style='width:150px;'>" + result.d[i].Id + "</td><td style='width:150px;'>" + result.d[i].Name
+ "</td><td style='width:150px;'>" + result.d[i].Surname + "</td><td style='width:150px;'>" + result.d[i].Sex
+ "</td><td style='width:150px;'>" + result.d[i].Email + "</td><td style='width:150px;'>" + result.d[i].City
+ "</td><td style='width:150px;'>" + result.d[i].Age + "</td></tr>");
}
},
error: function (result) {
alert("Unexpected result occured!");
},
complete: function() {
setTimeout(printValues,5000);
}
});
}
$(function() {
$("#div_sil").dialog();
$("#sil_name_error").dialog({
autoOpen: false,
buttons: [{
text: "OK",
click: function() { $(this).dialog("close"); }
}]
});
})
$("#ekle_button").click(function() {
$("#content").dialog("open");
$("#content").empty();
$("#content").load("Ekle.aspx #ekle");
});
$("#sil_button").click(function() {
$("#content").dialog("open");
$("#content").empty();
$("#content").load("Sil.aspx #div_sil");
});
$("#guncelle_button").click(function() {
$("#content").dialog("open");
$("#content").empty();
$("#content").load("Guncelle.aspx #div_guncelle");
});
$(function() {
$("#content").dialog({ autoOpen: false });
});
});
</script>
<body>
<form id="form1" runat="server">
<div id="div_goruntule">
<asp:GridView ID="gw1" runat="server" AutoGenerateColumns="false" RowStyle-BackColor="#A1DCF2"
HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="Id" HeaderText="Id" />
<asp:BoundField ItemStyle-Width="150px" DataField="Name" HeaderText="Name" />
<asp:BoundField ItemStyle-Width="150px" DataField="Surname" HeaderText="Surname" />
<asp:BoundField ItemStyle-Width="150px" DataField="Sex" HeaderText="Sex" />
<asp:BoundField ItemStyle-Width="150px" DataField="Email" HeaderText="Email" />
<asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
<asp:BoundField ItemStyle-Width="150px" DataField="Age" HeaderText="Age" />
</Columns>
</asp:GridView>
</div>
<div id="buttons">
<asp:Table ID="Table1" runat="server">
<asp:TableRow>
<asp:TableCell>
<input type="button" id="ekle_button" value="Ekle" />
<input type="button" id="sil_button" value="Sil" />
<input type="button" id="guncelle_button" value="Guncelle" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>
<div id="content">
</div>
<div id="ekle_part">
<div id="ekle_id_error">
<p>Please enter a valid id number.Id should include <b>only</b> numbers.</p>
</div>
<div id="ekle_name_error">
<p>Please enter a valid name.Name should include <b>only</b> characters, not numbers.</p>
</div>
<div id="ekle_surname_error">
<p>Please enter a valid surname.Surname should include <b>only</b> characters, not numbers.</p>
</div>
<div id="ekle_email_error">
<p>Please enter a valid email address.Email format should be [email protected]</p>
</div>
<div id="ekle_city_error">
<p>Please enter a valid city.City name should include <b>only</b> characters, not numbers</p>
</div>
<div id="ekle_age_error">
<p>Please enter a valid age number.Age should be include <b>only</b> numbers.</p>
</div>
</div>
<div id="sil_part">
<div id="sil_name_error">
<p>Name can not include numeric values.</p>
</div>
</div>
<div id="guncelle_part">
<div id="guncelle_id_error">Id contains only numbers.</div>
<div id="guncelle_name_error">Name can not be empty and can not be include numeric values.</div>
<div id="guncelle_surname_error">Surname can not be empty and can not include numeric values.</div>
<div id="guncelle_email_error">Email format should be like [email protected]</div>
<div id="guncelle_city_error">City can not be empty and can not be include numeric values.</div>
<div id="guncelle_age_error">Age contains only numbers</div>
</div>
</form>
下面是ekle.js:
$(document).ready(function() {
var result = 1;
var id, name, surname, email, city, age;
$("#ekle_gonder").click(function() {
var gender = document.getElementById('<%= ddl1.ClientID%>');
var gender2 = gender.options[gender.selectedIndex].value;
check();
var obj = {};
obj.Id = id;
obj.Name = name;
obj.Surname = surname;
obj.Sex = gender2;
obj.Email = email;
obj.City = city;
obj.Age = age;
if (result == 1) {
$.ajax({
type: "post",
url: "GridviewWithJquery.aspx/ekle_func",
contentType: "application/json;charset:utf-8",
data: JSON.stringify(obj),
dataType: "json",
success: onSuccess,
error: function (xhr, status, error) {
alert("no");
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
}
});
function check() {
var mailregex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-][email protected][a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
var textregex = /^[A-Za-z çğıöşü]{1,25}$/;
var numregex = /(^\d+$)/;
id = $("#txt1").val();
name = $("#txt2").val();
surname = $("#txt3").val();
email = $("#txt5").val();
city = $("#txt6").val();
age = $("#txt7").val();
if ((numregex.test(parseInt(id)) == false) || (parseInt(id) < 0)) {
open_error_box("#ekle_id_error", "#txt1");
}
if ((name == '') || (textregex.test(name) == false)) {
open_error_box("#ekle_name_error", "#txt2");
}
if ((surname == '') || (textregex.test(surname) == false)) {
open_error_box("#ekle_surname_error", "#txt3");
}
if ((email == '') || (mailregex.test(email) == false)) {
open_error_box("#ekle_email_error", "#txt5");
}
if ((city == '') || (textregex.test(city) == false)) {
open_error_box("#ekle_city_error", "#txt6");
}
if ((numregex.test(parseInt(age)) == false) || (parseInt(age) < 0) || (parseInt(age) > 100)) {
open_error_box("#ekle_age_error", "#txt7");
}
return result;
}
function open_error_box(error_name, name) {
$(error_name).dialog("open");
$(name).val('');
result = 0;
}
function onSuccess() {
$("#txt1").val('');
$("#txt2").val('');
$("#txt3").val('');
$("#txt4").val('');
$("#txt5").val('');
$("#txt6").val('');
$("#txt7").val('');
}
$(function() {
$("#ekle").dialog();
$("#ekle_id_error ,#ekle_name_error, #ekle_surname_error, #ekle_email_error, #ekle_city_error, #ekle_age_error").dialog(
{
autoOpen: false,
buttons: [{
text: "OK",
click: function() { $(this).dialog("close"); }
}]
});
})
});
我被困了幾個小時。
請告訴我一種在GridviewWithJquery.aspx
頁面中獲取加載項目值的方法嗎?
你已經有了一個*很多*的代碼在這裏,它不是最小的。把它燒到一個[MCVE](http://stackoverflow.com/help/mcve)。 – mason