简体   繁体   English

使用javascript无法获取输入字段值?

[英]Can't get input field value using javascript?

I seem to have a problem with getting field value using javascript and i think the facebook popup jquery script is blocking it but i don't know why... 我似乎有使用javascript获取字段值的问题,我认为facebook popup jquery脚本阻止它但我不知道为什么...

I would appreciate if someone could help me out because right now im about to tear my hair off. 如果有人可以帮助我,我会很感激,因为现在我要把我的头发撕掉。

<input type="text" id="photo_album_name" style="width: 280px; height: 20px; color: #525252;" />
<textarea id="photo_album_desc" style="width: 280px; height: 100px; color: #525252;"></textarea>

function submit_photoalbum()
{
    var albumname = document.getElementById('photo_album_name').value; 
    var albumdesc = document.getElementById('photo_album_desc').value;

    alert("Album name: "+ albumname + ", Album Desc: " + albumdesc);
}  

You can view a live example at http://goo.gl/op6r4 您可以在http://goo.gl/op6r4上查看实时示例

Your problem is that you have mutiple elements with the same id => invalid HTML 您的问题是您有多个具有相同id => 无效HTML的元素

<input type="text" id="photo_album_name" style="width: 280px; height: 20px; color: #525252;">
<input type="text" id="photo_album_name" style="width: 280px; height: 20px; color: #525252;">

<textarea id="photo_album_desc" style="width: 280px; height: 100px; color: #525252;"></textarea>
<textarea id="photo_album_desc" style="width: 280px; height: 100px; color: #525252;"></textarea>

You need to run your function on some event where the elements have value, or add value to them if you are running this on load. 您需要在元素具有值的某个事件上运行您的函数,或者如果您在加载时运行它,则需要为它们添加值。

FIDDLE 小提琴

change the input to: 将输入更改为:

<input class="photo_album_name" style="width: 280px; height: 20px; color: #525252;" type="text"/>

and

<textarea class="photo_album_desc" style="width: 280px; height: 100px; color: #525252;"/>

and your selector to: 和你的选择器:

   var albumname = $('#facebox').find('.photo_album_name').val(); 
   var albumdesc = $('#facebox').find('.photo_album_desc').val();

Note this duplicate ID also: 请注意此重复ID:

<input id="add" style="padding-bottom: 3px; padding-left: 5px; padding-right: 5px; font-size: 13px; margin-right: 15px; padding-top: 3px;" onclick="submit_photoalbum();" type="button" value="Create Photo Album"/>

change to: 改成:

<input class="add" style="padding-bottom: 3px; padding-left: 5px; padding-right: 5px; font-size: 13px; margin-right: 15px; padding-top: 3px;" onclick="submit_photoalbum();" type="button" value="Create Photo Album"/>

Ok.. It seems that you're replicating your form. 好的..好像你正在复制你的表格。 You have it on your HTML and when you call the facebox it replicates the hidden HTML. 您可以在HTML上使用它,当您调用facebox时,它会复制隐藏的HTML。 This replicates the ID's (as gdoron was stating). 这复制了ID(正如gdoron所说)。

It may be a good practice to use another framework, or to change the jQuery plugin to a more stable one (like http://fancybox.net ). 使用另一个框架或将jQuery插件更改为更稳定的插件(如http://fancybox.net )可能是一个好习惯。 Anyways. 无论如何。 You can change your submit_photoalbum function to: 您可以将submit_photoalbum函数更改为:

function submit_photoalbum()
{
    var albumname = $("#facebox > input").val(); 
    var albumdesc = $("#facebox > textarea").val();

    alert("Album name: "+ albumname + ", Album Desc: " + albumdesc);
} 

It's a bit hardcoded but it'll work for this. 它有点硬编码,但它会起作用。

既然你正在使用jQuery我只想尝试使用jQuery获取值,所以它应该是:

var albumname = $('#photo_album_name').val();

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

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