2012-01-29 139 views
0

我遇到了一些与我的代码有关的问题,也许你可以帮忙吗?将ID传递给选择器 - jquery

的Jquery:[更新]

<script> 
$(function() { 

    $(".val_error").dialog({ 
    autoOpen: false, 
    show: "blind", 
    hide: "explode" 
}); 
$(".val_open").click(function(event) { 
    var target = $(this).attr("id"); 
    $('#' + target).dialog('open'); 
    return false; 
}); 
    }); 
</script> 

HTML:[更新]

<p class="first_name> 
<div class="val_error" id="first_name_err"><?php echo form_error('first_name'); ?></div> 
<label for="contact_first_name"><?php echo $label_values->first_name;?></label> 
<?php echo form_input('first_name', $form_values->first_name, 'id="first_name"');?> 
<button class="val_open" id="first_name">Open</button> 
</p> 

<p class="last_name"> 
<div class="val_error" id="last_name_err"><?php echo form_error('last_name'); ?></div> 
<label for="contact_last_name"><?php echo $label_values->last_name;?></label> 
<?php echo form_input('last_name', $form_values->last_name, 'id="last_name"');?> 
<button class="val_open" id="last_name">Open</button> 
</p> 

所以基本上我试图让该对话框打开刚一次一个ID,而不是一次全部。我试过以下但没有运气:

Jquery的我想会的工作

<script> 
$(function() { 

    $(".val_error"+target).dialog({ 
     autoOpen: false, 
     show: "blind", 
     hide: "explode" 
    }); 
    $(".val_open").click(function(event) { 
      var target = $this.attr("id"); 
     $(".val_error").dialog("open"); 
     return false; 
    }); 
    }); 
</script> 

任何帮助/指针,甚至想法将是伟大的!

http://jsfiddle.net/dRRRd/ < - 可以在这里查看

+1

你的HTML是无效的,你不能有相同的ID两个(或更多)的元素。尝试定义和使用数据属性(即数据目标ID) – dievardump 2012-01-29 19:00:08

+0

@DieVarDump我已经排序,现在 – Sean 2012-01-29 19:22:37

回答

3
  1. 元素的ID必须是唯一的 - 你有两个first_name元素和两个last_name元素。这会导致问题。 (你也有两个标签“为” contact_name - 是否有这个ID两个元素呢?)

  2. 在JavaScript,target当你调用$(".val_error"+target).dialog({(它在另一个回调函数的范围内声明没有定义。 )

你想要做什么是指定一个类给每个窗体组的父元素,然后使用它作为一个选择器,找到你的错误的div。尝试是这样的:

<p class="first_name"> 
<div class="val_error" id="first_name_err"><?php echo form_error('first_name'); ?></div> 
<label for="contact_name"><?php echo $label_values->first_name;?></label> 
<?php echo form_input('first_name', $form_values->first_name, 'id="first_name"');?> 
<button class="val_open" id="first_name">Open</button> 
</p> 

<p class="last_name"> 
<div class="val_error" id="last_name_err"><?php echo form_error('last_name'); ?></div> 
<label for="contact_name"><?php echo $label_values->last_name;?></label> 
<?php echo form_input('last_name', $form_values->last_name, 'id="last_name"');?> 
<button class="val_open" id="last_name">Open</button> 
</p> 

然后你的jQuery选择将$(".first_name .val_error")$(".last_name .val_error")

+0

我怎么能使选择器动态呢?我已经重做了HTML,就像你建议我的JS好吗? 。'\t $( “val_error”)对话框({ \t \t的AutoOpen:假的, \t \t显示: “盲”, \t \t隐藏: “爆炸” \t}); 。 \t $( “val_open ”)点击(函数(事件){ \t \t VAR的目标= $(本).attr(“ ID”); \t \t $( '#' +目标).dialog(”开'); \t \t返回FALSE; \t});' – Sean 2012-01-29 19:16:10

+0

由于错误div的ID是基本目标的ID +‘_ERR’你** **可能只是硬编码的,但它可能会是更好的()。$(this).siblings(“。val_error”)。dialog('open'); return false;});''(未测试) – 2012-01-29 19:31:22

2

几件事情有以下行:

var target = $this.attr("id"); 
  1. $this将寻找一个名为$this变量,它不存在。要获取上下文相关的jQuery对象,请使用$(this)
  2. 变量target从不读取 - 也许您的意思是$('#' + target).dialog('open');在下一行?

但最简单的解决办法可能是删除:

var target = $this.attr("id"); 
$(".val_error").dialog("open"); 

..和其替换为:

$(this).dialog('open'); 

,因为只有一个元素反正得到click事件,该元素可以定位为$(this)