JS 未推送到数组

发布于 2024-09-10 19:31:05 字数 3648 浏览 5 评论 0原文

尝试将一些坐标以及用户在表单中指定的一些内容推送到名为“seatsArray”的数组中。这是我的代码:

<div>
<img onLoad="shiftzoom.add(this,{showcoords:true,relativecoords:true,zoom:100});" id="image" src="plan1.bmp" width="1024" height="768">
</div>

<script type="text/javascript">
var seatsArray = [];
</script>
<br><input id="backnave" type="radio" name="area" value="backnave" /> Back Nave<br>
<input id="frontnave" type="radio" name="area" value="frontnave" /> Front nave<br>
<input id="middlenave" type="radio" name="area" value="middlenave" /> Middle nave<br>
<input type="radio" id="standardseat" name="seat" /><label for="radio1">Standard</label><br>
<input type="radio" id="wheelchairseat" name="seat" checked="checked" /><label for="radio2">Wheelchair</label><br>
<form id="my_form" action="savetext.aspx" method="post" onsubmit="return prepare()">
  <input type="text" id="file_name" name="file_name" rows="1" cols="20" />
  <input type="hidden" name="seatsArray" />
  <input type="submit" value="Save" />
</form>

<form id="my_form" action="savetext.aspx" method="post" onsubmit="return prepare()">
  <input type="text" id="file_name" name="file_name" rows="1" cols="20" />
  <input type="hidden" name="seatsArray" />
  <input type="submit" value="Save" />
</form>

<script type="text/javascript">
function prepare();
{
  document.getElementById('seatsArray').value = seatsArray.join();
  return true;
}
</script>

<script type="text/javascript">
var coordinates = document.getElementById("image");
coordinates.onclick = function(e) {
e = e || window.event;

if (e && e.pageX && e.pageX) {
            e.posX = e.pageX;
            e.posY = e.pageY;

        } else if (e && e.clientX && e.clientY) {
            var scr     = {x:0,y:0},
                object  = e.srcElement || e.target;
            //legendary get scrolled
            for (;object.parentNode;object = object.parentNode) {
                scr['x'] += object.scrollLeft;
                scr['y'] += object.scrollTop;
            } 
            e.posX = e.clientX + scr.x;
            e.posY = e.clientY + scr.y;
        } 

var desc = "";
if(document.getElementByID("backnave").checked) {
  desc = "BN, "+desc;
} else if(document.getElementByID("middlenave").checked) {
  desc = "MN, "+desc;
} else if(document.getElementByID("frontnave").checked) {
  desc = "FN, "+desc;
}

if(document.getElementById('wheelchairseat').checked) {
  //Wheelchair seat is checked
  desc = "Wheelchair "+desc;
}

seatsArray.push(desc + e.posX, e.posY);

}
</script>

但根本没有任何内容被推送到数组中。我可以这么说,因为我正在使用以下 ASP.NET 将数组写入文本文件:

<script runat="server">
    protected void Page_Load(object sender, EventArgs e) 
    {
        string path = Server.MapPath(".")+"/"+Request.Form["file_name"] + ".txt";
        if (!File.Exists(path)) 
        {
            using (StreamWriter sw = File.CreateText(path)) 
            {
                sw.WriteLine(Request.Form["seatsArray"]);
                sw.WriteLine("");
            }   
        }

        using (StreamReader sr = File.OpenText(path)) 
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) 
            {
                Response.Write(s);
            }
        }
    }
</script>

根据用户在“file_name”形式中输入的内容,文本文件的名称是正确的。正如您所看到的,我将 SeatArray 设置为隐藏表单对象,这就是 ASP.NET 试图调用的对象。

javascript 中的某些内容是否顺序错误或其他什么?因为我无法让它填充该文本文件。

谢谢!

Trying to push some coordinates, as well as some stuff specified in a form by the user, to an an array called "seatsArray". Here's my code:

<div>
<img onLoad="shiftzoom.add(this,{showcoords:true,relativecoords:true,zoom:100});" id="image" src="plan1.bmp" width="1024" height="768">
</div>

<script type="text/javascript">
var seatsArray = [];
</script>
<br><input id="backnave" type="radio" name="area" value="backnave" /> Back Nave<br>
<input id="frontnave" type="radio" name="area" value="frontnave" /> Front nave<br>
<input id="middlenave" type="radio" name="area" value="middlenave" /> Middle nave<br>
<input type="radio" id="standardseat" name="seat" /><label for="radio1">Standard</label><br>
<input type="radio" id="wheelchairseat" name="seat" checked="checked" /><label for="radio2">Wheelchair</label><br>
<form id="my_form" action="savetext.aspx" method="post" onsubmit="return prepare()">
  <input type="text" id="file_name" name="file_name" rows="1" cols="20" />
  <input type="hidden" name="seatsArray" />
  <input type="submit" value="Save" />
</form>

<form id="my_form" action="savetext.aspx" method="post" onsubmit="return prepare()">
  <input type="text" id="file_name" name="file_name" rows="1" cols="20" />
  <input type="hidden" name="seatsArray" />
  <input type="submit" value="Save" />
</form>

<script type="text/javascript">
function prepare();
{
  document.getElementById('seatsArray').value = seatsArray.join();
  return true;
}
</script>

<script type="text/javascript">
var coordinates = document.getElementById("image");
coordinates.onclick = function(e) {
e = e || window.event;

if (e && e.pageX && e.pageX) {
            e.posX = e.pageX;
            e.posY = e.pageY;

        } else if (e && e.clientX && e.clientY) {
            var scr     = {x:0,y:0},
                object  = e.srcElement || e.target;
            //legendary get scrolled
            for (;object.parentNode;object = object.parentNode) {
                scr['x'] += object.scrollLeft;
                scr['y'] += object.scrollTop;
            } 
            e.posX = e.clientX + scr.x;
            e.posY = e.clientY + scr.y;
        } 

var desc = "";
if(document.getElementByID("backnave").checked) {
  desc = "BN, "+desc;
} else if(document.getElementByID("middlenave").checked) {
  desc = "MN, "+desc;
} else if(document.getElementByID("frontnave").checked) {
  desc = "FN, "+desc;
}

if(document.getElementById('wheelchairseat').checked) {
  //Wheelchair seat is checked
  desc = "Wheelchair "+desc;
}

seatsArray.push(desc + e.posX, e.posY);

}
</script>

But simply nothing is getting pushed to the array. I can tell this as I am using the following ASP.NET to write the array to a text file:

<script runat="server">
    protected void Page_Load(object sender, EventArgs e) 
    {
        string path = Server.MapPath(".")+"/"+Request.Form["file_name"] + ".txt";
        if (!File.Exists(path)) 
        {
            using (StreamWriter sw = File.CreateText(path)) 
            {
                sw.WriteLine(Request.Form["seatsArray"]);
                sw.WriteLine("");
            }   
        }

        using (StreamReader sr = File.OpenText(path)) 
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) 
            {
                Response.Write(s);
            }
        }
    }
</script>

The name of the text file is correct according to what the user put in the form "file_name". As you can see I made seatsArray a hidden form object so thats what the ASP.NET is trying to call.

Is something in the javascript in the wrong order or something? Because I just can't get it to fill up that text file.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

装纯掩盖桑 2024-09-17 19:31:05

在浏览器中,检查 JavaScript 错误。 Firefox/IE 报告 2。
你有一个不应该在那里的分号。 =>函数准备();

document.getElementById("图像");返回 null,因此坐标。onclick 失败

我建议在 Firefox 中使用 firebug 进行调试,或者使用 ie8 的开发人员工具(按 F12)

编辑:
确保在使用前将 SeatArray 声明为数组。

....

<脚本类型=“text/javascript”>

座位数组 = []; //插入这一行

函数prepare()
{

....

In the browser, check for javascript errors. Firefox/IE reports 2.
you have a semicolon that shouldn't be there. = > function prepare();

document.getElementById("image"); returns null, so coordinates.onclick fails

I recommend debugging in Firefox with firebug, or use developer tools with ie8 (press F12)

Edit:
make sure to declare seatsArray as an array before use.

....

< script type="text/javascript" >

seatsArray = []; //insert this line

function prepare()
{

....

假扮的天使 2024-09-17 19:31:05

尝试在您认为应该更改值之前和之后以及在提交表单之前的 javascript 中删除一些 alert(seatsArray) 语句。如果您看到正确的值,那么您就知道后端不正确。

经过进一步检查:prepare() 函数正在查找 id="seatsArray" 的元素,但该元素不存在。您有两个元素的 name="seatsArray" 但没有 id="seatsArray"。该数组很可能已被填充,但没有被写回表单。

Try dropping a few alert(seatsArray) statements in the javascript before and after you think it should change values and one right before the form is submitted. If you see the correct value then you know it's the back-end that is not right.

Upon further inspection: the prepare() function is looking for an element with id="seatsArray", which doesn't exist. You have two elements with name="seatsArray" but no id="seatsArray". The array is more than likely being populated but it is not being written back to the form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文