admin管理员组文章数量:1023195
function Myfunc(obj1)
{
alert(obj1.prop('outerHTML'));
}
<select id="myselect" onchange="MyFunc(jQuery(this))">
<option value="v1">Value 1</option>
<option value="v2">Value 2</option>
<option value="v3" selected="selected">Value 3</option>
</select>
function Myfunc(obj1)
{
alert(obj1.prop('outerHTML'));
}
<select id="myselect" onchange="MyFunc(jQuery(this))">
<option value="v1">Value 1</option>
<option value="v2">Value 2</option>
<option value="v3" selected="selected">Value 3</option>
</select>
I have the above two snippets. When i do alert of the object i get the plete <select&tg; tag along with all it's options.
But, What i want is the object passed should only just be the option which i have selected and not the plete select element block.
Share Improve this question asked Sep 30, 2014 at 9:28 PrashantPrashant 1351 gold badge2 silver badges8 bronze badges 1- yes i wanted the plete HTML of the option selected, and i am really thankful to @Regent for the answer. – Prashant Commented Nov 6, 2014 at 8:44
5 Answers
Reset to default 4Instead of outdated onchange
you can use jQuery $('selector').change(function() { });
Fiddle.
Simplified HTML:
<select id="myselect">
<option value="v1">Value 1</option>
<option value="v2">Value 2</option>
<option value="v3" selected="selected">Value 3</option>
</select>
JS:
$(document).ready(function()
{
$('#myselect').change(function()
{
alert($(this).find(':selected').text());
});
});
Update. If you need selected <option>
outer HTML, then it can be:
Fiddle.
$(document).ready(function()
{
$('#myselect').change(function()
{
alert($(this).find(':selected')[0].outerHTML);
});
});
Try:
onchange="MyFunc(jQuery(this).find('option:selected'))
"
Use as
function MyFunc(obj1)
{
alert($("#myselect option:selected").text());
}
DEMO
OR You can use
function MyFunc(obj1)
{
alert($("#myselect option:selected").html());
}
DEMO
You can pass object or you can fetch selected option in function.
To get selected option html in function use below code -
NOTE - please correct spelling of function either in onchange
or function declaration.
function MyFunc(obj1)
{
var value = obj1.value;
var optionHTML = $(obj1).find('option[value="'+value+'"]');
alert($('<div>').append(optionHTML.clone()).html());
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select id="myselect" onchange="MyFunc(this)">
<option value="v1">Value 1</option>
<option value="v2">Value 2</option>
<option value="v3" selected="selected">Value 3</option>
</select>
Another way is to bind change
event handler using jquery and get the option html
$('#myselect').change(function(){
var value = $(this).val();
var option = $(this).find('option[value="'+value+'"]');
var optionHTML = $('<div>').append(option.clone()).html();
alert(optionHTML);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<select id="myselect">
<option value="v1">Value 1</option>
<option value="v2">Value 2</option>
<option value="v3" selected="selected">Value 3</option>
</select>
You can use :selected
selector follows:
$(document).ready(function() {
$('#myselect').on('change', function() {
alert($(this).find('option:selected').eq(0));
});
});
function Myfunc(obj1)
{
alert(obj1.prop('outerHTML'));
}
<select id="myselect" onchange="MyFunc(jQuery(this))">
<option value="v1">Value 1</option>
<option value="v2">Value 2</option>
<option value="v3" selected="selected">Value 3</option>
</select>
function Myfunc(obj1)
{
alert(obj1.prop('outerHTML'));
}
<select id="myselect" onchange="MyFunc(jQuery(this))">
<option value="v1">Value 1</option>
<option value="v2">Value 2</option>
<option value="v3" selected="selected">Value 3</option>
</select>
I have the above two snippets. When i do alert of the object i get the plete <select&tg; tag along with all it's options.
But, What i want is the object passed should only just be the option which i have selected and not the plete select element block.
Share Improve this question asked Sep 30, 2014 at 9:28 PrashantPrashant 1351 gold badge2 silver badges8 bronze badges 1- yes i wanted the plete HTML of the option selected, and i am really thankful to @Regent for the answer. – Prashant Commented Nov 6, 2014 at 8:44
5 Answers
Reset to default 4Instead of outdated onchange
you can use jQuery $('selector').change(function() { });
Fiddle.
Simplified HTML:
<select id="myselect">
<option value="v1">Value 1</option>
<option value="v2">Value 2</option>
<option value="v3" selected="selected">Value 3</option>
</select>
JS:
$(document).ready(function()
{
$('#myselect').change(function()
{
alert($(this).find(':selected').text());
});
});
Update. If you need selected <option>
outer HTML, then it can be:
Fiddle.
$(document).ready(function()
{
$('#myselect').change(function()
{
alert($(this).find(':selected')[0].outerHTML);
});
});
Try:
onchange="MyFunc(jQuery(this).find('option:selected'))
"
Use as
function MyFunc(obj1)
{
alert($("#myselect option:selected").text());
}
DEMO
OR You can use
function MyFunc(obj1)
{
alert($("#myselect option:selected").html());
}
DEMO
You can pass object or you can fetch selected option in function.
To get selected option html in function use below code -
NOTE - please correct spelling of function either in onchange
or function declaration.
function MyFunc(obj1)
{
var value = obj1.value;
var optionHTML = $(obj1).find('option[value="'+value+'"]');
alert($('<div>').append(optionHTML.clone()).html());
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select id="myselect" onchange="MyFunc(this)">
<option value="v1">Value 1</option>
<option value="v2">Value 2</option>
<option value="v3" selected="selected">Value 3</option>
</select>
Another way is to bind change
event handler using jquery and get the option html
$('#myselect').change(function(){
var value = $(this).val();
var option = $(this).find('option[value="'+value+'"]');
var optionHTML = $('<div>').append(option.clone()).html();
alert(optionHTML);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<select id="myselect">
<option value="v1">Value 1</option>
<option value="v2">Value 2</option>
<option value="v3" selected="selected">Value 3</option>
</select>
You can use :selected
selector follows:
$(document).ready(function() {
$('#myselect').on('change', function() {
alert($(this).find('option:selected').eq(0));
});
});
本文标签: jqueryCall a javascript function from select element in HTMLStack Overflow
版权声明:本文标题:jquery - Call a javascript function from select element in HTML - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745510567a2153807.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论