简体   繁体   English

如何从选项标签值属性替换值

[英]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 Quotesescape 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();

Check Fiddle 检查小提琴

使用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.

相关问题 我如何将 attr 从 choisen 选项标签更改为值? - How i can change attr to value from choisen option tag? 如何使用jQuery替换选项标签中的特定值 - how to replace specfic value from the option tag using jquery 如何从中检索循环值属性 <option>标记并传递到中的onchange属性<select>标签 - How to retrieve looped value attributes from <option> tag and pass to the onchange attribute in <select> tag 我怎样才能从期权中获得价值? - How can I get value from option? 如何使用 JavaScript\\JQuery 使用选择标记的选定选项的值设置隐藏输入标记的值? - How can I set the value of an hidden input tag with the value of the selected option of a select tag using JavaScript\JQuery? 如何使用正则表达式从特定标签的属性获取值? - How can I use a Regex to get the value from an attribute from a specific tag? 根据选项“label”属性替换HTML中的值 - Replace value in HTML based on option “label” attribute 我如何使用子值获取父标记属性href值 - how can i get the parent a tag attribute href value using child value 使用Dalekjs测试工具,当Option标记中没有“值”属性时,如何在Dropdown(选择元素)中选择Option? - Using Dalekjs test tool, how to select Option in Dropdown(Select element) when there is no “value” attribute in Option tag? 如何获取所选选项的值(如显示值而非值=“”) - How can i get the value (as in displayed value not value=“”) of selected option
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM