Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 替换多个元素上的字符_Javascript_Jquery - Fatal编程技术网

Javascript 替换多个元素上的字符

Javascript 替换多个元素上的字符,javascript,jquery,Javascript,Jquery,考虑到这段HTML <table class="variations" cellspacing="0"> <tbody> <tr> <td class="label"> <label for="cantidad">Cantidad</label> </td> <td class="value"> &

考虑到这段HTML

<table class="variations" cellspacing="0">
<tbody>
    <tr>
        <td class="label">
            <label for="cantidad">Cantidad</label>
        </td>
        <td class="value">
            <fieldset> 
                <br />
                <input type="radio" value="3-rosas" id="cantidad" name="attribute_cantidad">3_rosas
                <br />
                <input type="radio" value="12-rosas" checked='checked' id="cantidad" name="attribute_cantidad">12_rosas
                <br />
                <input type="radio" value="18-rosas" id="cantidad" name="attribute_cantidad">18_rosas
                <br />
                <input type="radio" value="24-rosas" id="cantidad" name="attribute_cantidad">24_rosas
                <br />
            </fieldset>
        </td>
    </tr>
</tbody>
</table>

康蒂达

3_rosas
12_rosas
18_rosas
24_rosas
我希望使用javascript或jQuery将选项文本上的“ux”字符替换为空白

我试过了,但没有成功


谢谢。

很难在单选按钮后获取每个文本,然后用空格替换
。您需要将文本包装在一些html元素中,如
label
span
等,然后使用
.replace(“”,“”);)

请参阅下面的代码-

HTML-我已将文本包装到标签中

<table class="variations" cellspacing="0">
  <tbody>
    <tr>
      <td class="label">
         <label for="cantidad">Cantidad</label>
      </td>
      <td class="value">
         <fieldset> 
            <br />
            <input type="radio" value="3-rosas" id="cantidad" 
                    name="attribute_cantidad">
            <label>3_rosas</label>
            <br />
            <input type="radio" value="12-rosas" checked='checked' 
                   id="cantidad" name="attribute_cantidad">
            <label>12_rosas</label>
            <br />
            <input type="radio" value="18-rosas" id="cantidad"
                   name="attribute_cantidad">
            <label>18_rosas</label>
            <br />
            <input type="radio" value="24-rosas" id="cantidad" 
            name="attribute_cantidad">
            <label>24_rosas</label>
            <br />
         </fieldset>
       </td>
    </tr>
</tbody>

请检查下面的代码

$('.value').find('input:radio').each(function(){
    var val = $(this).val().replace(/-/g, ' ');
    $(this).next('span').html(val);
});
请检查下面的小提琴


您可以使用
.contents()
方法,然后按如下方式替换每个文本节点中的任何
\uu

$('.value > fieldset').contents().each(function() {
    this.nodeType !== 3 || ( this.textContent = this.textContent.replace(/_/g,' ') );
});
$('.value>fieldset').contents().each(函数(){
this.nodeType!==3 | |(this.textContent=this.textContent.replace(/g/,);
});

康蒂达

3_rosas
12_rosas
18_rosas
24_rosas

您尝试的jquery代码在哪里?选项文本?那是哪里?我不认为有多个同名的身份证是好的。谢谢,我用这个把头撞到墙上了。我没有用标签包装元素。很高兴帮助您:)
$('.value > fieldset').contents().each(function() {
    this.nodeType !== 3 || ( this.textContent = this.textContent.replace(/_/g,' ') );
});