jQuery's .val() 在 Facebox 中不起作用 [英] jQuery's .val() not working in Facebox

查看:34
本文介绍了jQuery's .val() 在 Facebox 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Facebox (http://defunkt.github.com/facebox/) 在我的一个网站上.我也在网站上广泛使用 jQuery.

我的问题是 .val() 函数似乎在 facebox 中不起作用.现在,这是显示为 facebox 的 DIV:

现在,问题是 input.tags-input 的值没有显示在警告框中.它显示为空.

让问题变得更糟的是,jQuery 选择实际上是有效的.也就是说,$('input.tags-input').hide() 工作得很好.

让问题变得更糟的是,.val() 使用初始值.也就是说,如果我使用这个:

然后,无论我是否更改值,警报框都会显示Some Value".

我完全被困在这里.代码中相同位置的 .val() 与 facebox 外的文本框一起工作正常.

解决方案

正如 Ngo Minh Nam 所指出的,facebox将复制原件div,所以你正在阅读初始输入字段的值.

这是我的(丑陋的)解决方法一切正常:

$('#input_field_id').live('keyup', function() {$('#input_field_id').val($(this).val());});

这将更新输入字段每次在里面按下一个键值框.

更新:忘记了!只需在要读取的元素的 id 之前附加 #facebox.在我的例子中:

$('#facebox #input_field_id').val();

还请注意,您必须将这些行包裹在

 $(document).bind('reveal.facebox', function() {

I'm using Facebox (http://defunkt.github.com/facebox/) on one of my websites. Also i'm using jQuery extensively on the site.

My problem is that the .val() function doesn't seem to work inside facebox. Now, this is the DIV that shows up as facebox :

<div id="edit-tags" style="display: none;">
  <script type="text/javascript">   
   $('.add-tag-form').submit(function(){
    alert($('input.tags-input').val());
    return false;
   });
  </script>

  <form class="add-tag-form">
   <input type="text" name="tags-input" class="tags-input" />
   <input type="submit" class="small-button" value="Add Tag" />
  <form>
</div>

Now, the problem is that the value of input.tags-input doesn't show up on the alert box. It shows up empty.

To make the problem worse, the jQuery selection actually works. That is, $('input.tags-input').hide() works perfectly fine.

To make the problem even worse, the .val() works with the initial value. That is, if i use this :

<input type="text" name="tags-input" class="tags-input" value="Some value" />

Then, the alert box shows "Some Value" irrespective of whether i change the value or not.

I'm totally stuck here. The .val() at the same position in the code works fine with the text boxes outside the facebox.

解决方案

As Ngo Minh Nam pointed out, facebox will make a clone of the original div, so you're reading the initial value of the input field.

Here's my (ugly) workaround to make things work:

$('#input_field_id').live('keyup', function() {
  $('#input_field_id').val($(this).val());
});

This will update the input field everytime a key is depressed inside the value box.

UPDATE : forget that! simply append #facebox before the id of the element you want to read. In my example:

$('#facebox #input_field_id').val();

Please also note that you have to wrap these lines inside the

    $(document).bind('reveal.facebox', function() { 

这篇关于jQuery&amp;#39;s .val() 在 Facebox 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆