简体   繁体   English

如何将参数传递给此jQuery?

[英]How to pass a parameters to this jquery?

i dont have a clue how i should rewrite this jquery so i can pass parameters into it. 我不知道我应该如何重写此jQuery,以便我可以将参数传递给它。 i want to pass an id into the function so i place it like, $(".dialog" + id) . 我想将id传递给函数,所以我将其放置为$(".dialog" + id)

i am goin to trigger it with a <a> 我要用<a>触发它

$(document).ready(function() {                 
  $(".btnCheck").live("click", function(evt) {
    evt.preventDefault();                               
    $(".dialog").dialog({
      height: 700,
      width: 600,
      closeOnEscape: true,
      title: 'prev'
    }).dialog("open");
  });           
}); 

assuming that what you want is the ID of "btnCheck. 假设您想要的是“ btnCheck”的ID。

$(document).ready(function() {                 
        $(".btnCheck").live("click", function(evt) {
            evt.preventDefault();                               

            $(".dialog" + $(this).attr("id")).dialog({ height: 700, width: 600, closeOnEscape: true, title: 'prev' }).dialog("open");
        });           
    });

Assuming: 假设:

<input type="button" class="btnCheck" id="btn1" value="Click Me">
...
<div id="dialog-btn1" class="dialog">
  ...
</div>

you can do: 你可以做:

$(function() {
  $(":button.btnCheck").live("click", function(e) {
    $("#dialog-" + this.id).dialog({...});
    e.preventDefault();
  });
});

You'll note that instead of using a dynamic class name, I gave the div a class of dialog. 您会注意到,我没有使用动态的类名,而是给div一个对话框类。 To get a specific one, since you will only ever want one (at a time), an ID is a better choice (imho). 要获得一个特定的身份,因为一次(一次)只需要一个,ID是一个更好的选择(恕我直言)。

this within this event handler refers to the button that was clicked ie the source of the event. this事件处理程序中的this表示单击的按钮,即事件的来源。

You need to associate your buttons with you dialogs somehow. 您需要以某种方式将按钮与对话框关联。

If you give the buttons a class of button and the dialogs a class dialog. 如果为按钮提供一个按钮类,而为对话框提供一个类对话框。 You can then get all this into an array and loop through them the first button in the array will activate the dialog which has an id of dialog1. 然后,您可以将所有这些放入数组中并遍历它们,数组中的第一个按钮将激活ID为dialog1的对话框。

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

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