[英]How can i remove HTML nbsp tags from select options when assigning the text of option to hidden input
When the value is assigned to html hidden element it also assign nbsp tag which is in select statement. 将值分配给html hidden元素时,它还会分配nbsp标记,该标记位于select语句中。 I don't want any html tags just only text
我不希望任何html标签只是文本
function setTextFieldA(ddl) { var a = ddl.options[ddl.selectedIndex].text; var res = a.slice(0,12); alert(res); document.getElementById('prod1').value = res; /*document.getElementById('prod1').value = ddl.options[ddl.selectedIndex].text;*/ }
<select name="1" id="1" class="selectadd" onchange="setTextFieldA(this)" required > <option value="99">Red Vein </option> <option value="99">White Vein </option> <option value="99">Green Vein </option> <option value="109">Red Horned [Add $10]</option> <option value="109">White Horned [Add $10]</option> <option value="109">Green Horned [Add $10]</option> <option value="109">Maeng da [Add $10]</option> </select> <input id="prod1" type = "hidden" name = "prod1" value = "Red Vein" />
First, do not use slice, you never know what length your value may be. 首先,不要使用切片,你永远不知道你的价值可能是多长。 Remove
var res = a.slice(0,12);
删除
var res = a.slice(0,12);
Second, you can use regular expression to remove all white spaces and the price tag at the end of the string, like this: 其次,您可以使用正则表达式删除字符串末尾的所有空格和价格标签,如下所示:
var res = res.replace(/\s*(\[.*\])*$/, ""); //removes white spaces and "[Add 10$]" tag
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.