开发者

Use click event for input type image using jquery

开发者 https://www.devze.com 2023-02-23 04:25 出处:网络
I am loading page data using ajax. Where I want to run a function on a click for an input,whose type is image and its id starts with \'SubmitVote\' followed by some integer. Below is the code, which i

I am loading page data using ajax. Where I want to run a function on a click for an input,whose type is image and its id starts with 'SubmitVote' followed by some integer. Below is the code, which is nt working.

$(function(开发者_Go百科) {
    $('input:image[id^="submitVote"').live('click',function(){
        if(this.id.match(/^submitVote(\d+)$/)){
            var vote=$("#rtngCmb").val();
            var text=$("#userNote").val();
            var alertId=RegExp.$1;

            postVote(alertId, vote, text);
        }
    });
});

how it can be done?


If you can't modify the markup to include a class, try the following:

$('input[type="image"][id^="submitVote"]').live('click', function() {
    if (this.id.match(/^submitVote(\d+)$/)) {
        var vote = $("#rtngCmb").val();
        var text = $("#userNote").val();
        var alertId = RegExp.$1;

        postVote(alertId, vote, text);
    }
});

Simple example on jsfiddle

Also, the reason your did not work the last ] was missing in your selector.


You best bet is to give all of your images that start with submitVote a specific class

<input id='submitVote1' type='image' src='image.png' class='submit' />
<input id='submitVote2' type='image' src='image.png' class='submit' />

This way you can simply do...

$('input.submit').live('click',function(){
        var vote=$("#rtngCmb").val();
        var text=$("#userNote").val();
        var alertId=RegExp.$1;
        postVote(alertId, vote, text);
});
0

精彩评论

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

关注公众号