开发者

How can I get the remainder of an ID when using jQuery attributes starts with

开发者 https://www.devze.com 2023-02-25 08:42 出处:网络
This is a foll开发者_JS百科ow up to my last question. Thank you very much for those answers. I had a bit more to ask so here it is in another question

This is a foll开发者_JS百科ow up to my last question. Thank you very much for those answers. I had a bit more to ask so here it is in another question

If I use:

 $("[id^=topic]").change(function() { ... }); 

This gives me an ID that starts with topic. However I would like to find out what's left in the ID and then add this to something else.

In other words if an element with ID topic_1 changes then I would like to get the value of the element with ID key_1

How can I get the "_1" and add this.

Based on the suggestions I tried:

<script src="/Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
$("[id^=topic]").change(function() { 

var n= $(this).attr("id").split("topic_")[0];
alert(n);

}); 

and in my code:

<select id="topic_1">
                    <option selected="selected" value="cat" >cat</option>
                    <option value="dog">dog</option>
</select>

But nothing happens when I change the dropdown. Am I missing something?


$("[id^=topic]").change(function() { 

var n= $(this).attr("id").split("topic_")[0];

}); 


Inside function use this to get the current 'changed' item

$("[id^=topic]").change(function() { 
  alert($(this).attr('id').replace('topic', ''));  
}); 

EXAMPLE HERE.

0

精彩评论

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

关注公众号