I have a javascript
object in .aspx
page which have some parameters whose values needed to be fetched from .aspx
fields on page load .
This is the javascript
object-
<script type='text/javascript'>
trackingObject = {text1:"",text2:"",Date1:""};</script>
The values of text1
, text2
, Date1
needs to replaced from .aspx
page fields(like from textbox
).
How can I achieve this with the help of RegisterStartupScript
or any other easy and alternative method?
First of all, assign Id for those controls, then
<script type='text/javascript'>
var text1Text = document.getElementById('text1Id').value;
var text2Text = document.getElementById('text2Id').value;
var date1Text = document.getElementById('date1Id').value;
var obj = {text1:text1Text, text2:text2Text, Date1:date1Text };
</script>
try this
<asp:TextBox runat="server" ID="txt1" runat="server"/>
// you can get this text box value by jquery like this
<script type='text/javascript'>
var txt1val = $('#<%= txt1.ClientID %>').val();
</script>
<script type='text/javascript'>
var text1Text = $('#<%= yourAspTextbox1ID.ClientID %>').val();
var text2Text = $('#<%= yourAspTextbox2ID.ClientID %>').val();
var text3Text = $('#<%= yourAspTextbox3ID.ClientID %>').val();
var obj = { text1: text1Text, text2: text2Text, Date1: text3Text};
</script>
Using jQuery you can do this:
<input type="text" /><br />
<input type="text" /><br />
<input type="text" /><br />
<input type="button" value="Show Obj"/>
$("input[type='button']").on("click", function () {
var values = [];
$("input[type='text']").each(function () {
values.push($(this).val());
});
trackingObject = {
text1: values[0],
text2: values[1],
Date1: values[2]
};
alert(JSON.stringify(trackingObject, null, 4));
});
尝试这个
Page.RegisterClientScriptBlock("test", "<script type='text/javascript'>$(document).ready(function(){trackingObject = {text1:'" + txtbox1.Text + "',text2:'" + txtbox2.Text + "',Date1:'" + txtDate.Text + "'};}) ;</script>");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.