如何有不同的链接 [英] How to have different link
问题描述
我是JS语言的新手> jQuery> select2和...英语! 我的问题:我希望 http://jsfiddle.net/JfWtJ/1/中的链接是动态的.
I am new in JS > jQuery > select2 and ... english language! My problem: I would like that the link in http://jsfiddle.net/JfWtJ/1/ is dynamic.
因此,我第一次进行测试:
So, in first time, I test :
function format(state) {
return state.text + "<a class='info' target='_blank' href='http://obs43/test.php?param="+state.id+"' >link</a>";
}
(请参阅 http://jsfiddle.net/7J8Ag/),但这不是可以的.
(see http://jsfiddle.net/7J8Ag/) but it's not ok.
第二次,我使用 http://jsfiddle.net/7J8Ag/1/ :
$("#select").on('open', function() {
$('.select2-results i').on('mouseup', function() {
$('#select').on('change', function() {
window.open("http://obs43/test.php?param=" + $("#select").val());
});
});
});
第一次选择就可以,但是第二个选择有两个"window.open",第三个选择有三个"window.open",依此类推.
It's ok for the first select, but at the second select, there are two "window.open", at the third select, there are three "window.open", etc.
为什么?如何正确执行此操作?
Why? How do I do this correctly?
我指定我希望"window.open ..."以 ONLY 开头以单击链接",而不是以"$('#select').on('change' ..."
I specify that I want that "window.open..." start with ONLY to click on "link" and not with "$('#select').on('change'..."
我使用第二次写是因为
$("#select").on('open', function() {
$('.select2-results i').on('mouseup', function() {
window.open("http://obs43/test.php?param=" + $("#select").val());
}); "
看起来不错,但是如果您看到的很精确,则说明$(#select").val()是优先项,而不是列表中选择的$(#select").val().而且这最后一个原则在完整列表中是正确的,但是在第一次搜索后使用列表时就不行了...
seem ok but if you see precisely, you remark that $("#select").val() is the precendly item, and not $("#select").val() of the choice in the list. And this last princip is correct in the full list but it's not ok when you use the list after a first search...
推荐答案
使用此js可以解决您的问题
Use this js it will fix your problem
function format(state) {
// if (!state.id) return state.text; // optgroup
return state.text + "<i class='info' id='"+state.id+"'>link</i>"+state.id;
//return state.text + "<a class='info' target='_blank' href='http://obs43/test.php?param="+state.id+" >link</a>";
}
$("#select").select2({
formatResult: format,
escapeMarkup: function(m) { return m; }
});
$("#select").on('open', function() {
$('.select2-results i').on('mouseup', function(event) {
// alert(event.target.id);
window.open("http://obs43/test.php?param=" +event.target.id);
});
});
添加此功能
$("#select").on('open', function() {
$('.select2-results i').on('mouseup', function(event) {
// alert(event.target.id);
window.open("http://obs43/test.php?param=" +event.target.id);
});
});
这篇关于如何有不同的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!