开发者

Jquery - How to get click event within hidden div?

I am using jquery and the action Im trying to get is when a person clicks a button I have it slide down a hidden div underneath it and display the hidden div. Inside that div there is a form field with a submit button. How would I go about listening for the event of someone clicking the submit button inside that div? I cant seem to get it to work:

UPDATE: This seem to work, let me know if this is the proper way:

$('.button.green.table').live('click',function() {

    var dataString = $(开发者_StackOverflow中文版this).attr('title');

    $("#formdiv" + dataString).slideToggle("fast", function() {

    //Added, this works
            $(this).find('.button').click(function() {

    alert($('input[name$="notes"]').val());

    return false;

    });

        return false;

    });

});

EDIT SOME HTML:

<button class="button green table" title="1">Add Notes</button>
<div id="formdiv1">Notes<br /><input type="text" name="notes" value="12" />
<button class="button" id="buttontest">GO</button></div>


I'd begin by taking the buttons click event out of the forms slideToggle event as it doesn't belong there.

also, are you sure the id's are correct once the page is rendered? I always use the class selector and never the Id because in asp.net the id's can change. you don't specify what you are using

edit

$("#formdiv" + dataString + " .button").click(function(){
  //your code here
});

or

$("#formdiv" + dataString + " .button").live("click",function(){
  //your code here
});


Try something like this:

http://jsfiddle.net/nC67m/

Using the selector

$(':input[name=notes]', $('#formdiv' + dataString)).val()

I would recommend not doing it the way you are doing it though. I would actually use classes probably with some selection on the parent. This way it's more general and you can have as many as you want without selecting with an id. Simpler code as well.

So you could do something like:

http://jsfiddle.net/nC67m/1/

note delegate works just like live, but better.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜