[英]How can i replace value from the option tag value attribute
I have a below scenario. 我有以下情况。 Actually, by the process the after rendering the html dropdown box is display the value like below
实际上,通过此过程,渲染html下拉框后将显示如下所示的值
<select id="ht" class="input-box quick-jump-menu" name="ht">
<option selected="selected" value="">Jump straight to a below option</option>
<option value="<span id="_SE_CP" _SE_CPt="default"><a href="Google.co.uk" alt="Lakeside" target="_self" class="">Lakeside</a></span>">
Lakeside
</option>
<option value="<span id="_SE_CP" _SE_CPt="default"><a href="http://www.google.com" alt="Alvaston Hall" target="_self" class=""></a></span>">
Alvaston
</option>
by some process, it is using some span tag there, i need only url in the value attribute like below 通过某些过程,它在那里使用了一些span标签,我只需要在value属性中使用url,如下所示
<select id="ht" class="input-box quick-jump-menu" name="ht">
<option selected="selected" value="">Jump straight to a below option</option>
<option value="Google.co.uk">
Lakeside
</option>
<option value="http://www.google.com">
Alvaston
</option>
Here, i need only set the URL value in the "Value" attribute of the "option" tag. 在这里,我只需要在“选项”标签的“值”属性中设置URL值。 i think this can be done through jquery.
我认为这可以通过jquery完成。
Please suggest any one if possible and help will be much appreciated 如果可能的话,请提出任何建议,我们将不胜感激
If you're rendering the option tag like this and you want to change it on the client side, here's a quick but dirty solution: 如果要渲染这样的选项标签,并且想在客户端进行更改,这是一个快速但肮脏的解决方案:
$("#ht option").each(function(){
$(this).attr("value", $($(this).attr("value")).find("a").attr("href"));
});
var tempDiv=$('<div/>');
$('#ht option').each(function(){
tempDiv.html($(this).val());
$(this).val(tempDiv.find('a').attr('href'));
});
The above code uses a temporary div (tempDiv) to capture the html from the option value and extract the href value. 上面的代码使用临时div(tempDiv)从选项值捕获html并提取href值。
Demo (select an option): http://jsfiddle.net/A7nyd/ 演示(选择一个选项): http : //jsfiddle.net/A7nyd/
[Update] this is actually the same answer as @chrisgonzalez. [更新]这实际上是与@chrisgonzalez相同的答案。
First off there is an error with the current HTML.. All the html inside of the value have double quotes.
首先,当前的HTML出现错误。值中的所有html都带有
double quotes.
Encase that inside single Quotes
or escape the ones inside
. 将其括在
single Quotes
或escape the ones inside
括起来。
Try this 尝试这个
$('#ht').after('<div id="check"></div>');
$('#ht option').each(function(){
if(this.value != ''){
var $check = $('#check');
var str = this.value;
$check.append(str);
var href = $check.find('a').attr('href');
this.value = href;
$check.empty();
}
});
$('#check').remove();
使用jQuery:
$('option').attr({value: 'new src'});
Try this: 尝试这个:
$("#ht option").each(function()
{
$(this).val($(this).val().find("a").attr("href"));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.