admin管理员组文章数量:1404947
I have a few select elements that are grouped by a span. I'm creating a plugin to do some interaction with the elements. Now I would like to give my users the support of the val()
function, so that they are able to get or set the 'value' of my span. Setting the value will result in the select box element to change en getting the value will result in the addition of the selectbox values.
Basically I would like my plugin to add the support for the val()
method. Any ideas on how to implement this?
Code
<span id="test">
<select id="one">
<option value="1">1</option>
<option value="2">2</option>
</select>
<select id="two">
<option value="1">1</option>
<option value="2">2</option>
</select>
</span>
Challange
Get the following code to work: $('#test').val('1:1');
and $('#test').val()
.
I have a few select elements that are grouped by a span. I'm creating a plugin to do some interaction with the elements. Now I would like to give my users the support of the val()
function, so that they are able to get or set the 'value' of my span. Setting the value will result in the select box element to change en getting the value will result in the addition of the selectbox values.
Basically I would like my plugin to add the support for the val()
method. Any ideas on how to implement this?
Code
<span id="test">
<select id="one">
<option value="1">1</option>
<option value="2">2</option>
</select>
<select id="two">
<option value="1">1</option>
<option value="2">2</option>
</select>
</span>
Challange
Get the following code to work: $('#test').val('1:1');
and $('#test').val()
.
-
1
I'm not sure whether it's a good idea to override
val()
(as you will have to fiddle with what the original.val()
does) but +1 for an interesting question – Pekka Commented May 5, 2011 at 9:26 - Okay the main question is how to override val() not how to implement your specific task? – DanielB Commented May 5, 2011 at 9:48
-
1
@DanielB: true. I would like to override, support or otherwise implement the
val()
in my plugin. – Kees C. Bakker Commented May 5, 2011 at 9:52
5 Answers
Reset to default 2This is not a full plugin and I did not override val()
but it should do what you want.
$.fn.value = function(value) {
if (value) {
var s = value.split(':');
for ( var i = 0; i < s.length; i++ ) {
this.find('select').eq(i).val(s[i]);
}
} else {
var result = [];
this.find('select').each( function( index, item ) {
result.push($(item).val());
});
return result.join(':');
}
}
$(function() {
$("#test").value("2:2");
alert($("#test").value());
});
You can try it at http://jsfiddle/QBSWm/1/
I don't think its a good idea to mess with jQuery like this, and there may be some typing errors, but here you go:
var oldVal = jQuery.fn.val;
jQuery.fn.extend({
val: function(value) {
// replace class check below with something related to your span
if (this.length == 1 && this.is('span.my-custom-class')) {
if (arguments.length == 0) {
// below is just a sample, traverse
// child selects like in DanielB's answer
// and return the value you expect
return this.attr('justsomesample');
}
else {
this.attr('justsomesample', value);
return this;
}
};
return oldVal.apply(this, arguments); }});
});
Look at the InputMask jquery plugin. What they do is to store original jquery val()
function in a variable and replace it with their own. Then you will receive the call first, you can check if the element is span
, and if so return it otherwise call the original function.
var originalVal = $.fn.val;
$.fn.val = function(params){
// if span then return your logic
// otherwise call originalVal
}
You can use the .text() JQuery function instead of .val() JQuery function to change your span.
It's not a good iead to override .val()
, but if you really want a .val()
like method for span, you could just add following code :
$.fn.sval = function(value) {
if(!value)
return $(this).html();
else
return $(this).html(value);
}
and then, to use it, just like .val() :
// Get value
alert($('#test').sval());
// Set value
$('#test').sval("yo");
本文标签: javascriptHow to implement jQuery val() for a spanStack Overflow
版权声明:本文标题:javascript - How to implement jQuery val() for a span? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744857918a2628887.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论