Javascript错误信息显示str.replace不是一个函数



我有一个返回字符串的问题,其中值"奶酪"替换为"jam"。我的场景首先从要选择的奶酪和蘑菇的默认值开始。如果我想选择奶酪,西红柿,蘑菇,控制台日志,我需要值"奶酪"替换为"jam",则控制台日志结果应为"jam,tomatoes,mushroom"。当我点击检查值按钮时,错误信息显示控制台日志中的str.replace is not a function

下面是我的示例代码:

$('#select_multiple').multiselect()
var data="cheese,mushrooms";
var dataarray=data.split(",");
$("#select_multiple").val(dataarray);
$("#select_multiple").multiselect("refresh");   
function check_value(){
var str = $('#select_multiple').val();  
var abc = str.replace("cheese", "jam"); 
console.log(abc);
}
<!DOCTYPE html>
<html>
<head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/css/bootstrap-multiselect.css" />
</head>
<body>
<select id="select_multiple" multiple="multiple">
<option value="cheese">Cheese</option>
<option value="tomatoes">Tomatoes</option>
<option value="mozarella">Mozzarella</option>
<option value="mushrooms">Mushrooms</option>
<option value="pepperoni">Pepperoni</option>
<option value="onions">Onions</option>
</select>
<button onclick="check_value();">Check Value</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/js/bootstrap-multiselect.min.js"></script>
</body>
</html>

我试图提醒变量str检查值,它得到值。只是不能控制台记录值。希望有人能指导我如何解决这个问题。谢谢。

这是因为您正在获得str作为数组。你需要使用for循环来替换你想要的字符串。参见下面的示例

$('#select_multiple').multiselect()
var data="cheese,mushrooms";
var dataarray=data.split(",");
$("#select_multiple").val(dataarray);
$("#select_multiple").multiselect("refresh");   
function check_value(){
var str = $('#select_multiple').val(); 
for(var i=0; i < str.length; i++) {
str[i] = str[i].replace('cheese', 'jam');
}    
console.log(str)
}
<!DOCTYPE html>
<html>
<head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/css/bootstrap-multiselect.css" />
</head>
<body>
<select id="select_multiple" multiple="multiple">
<option value="cheese">Cheese</option>
<option value="tomatoes">Tomatoes</option>
<option value="mozarella">Mozzarella</option>
<option value="mushrooms">Mushrooms</option>
<option value="pepperoni">Pepperoni</option>
<option value="onions">Onions</option>
</select>
<button onclick="check_value();">Check Value</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/js/bootstrap-multiselect.min.js"></script>
</body>
</html>

这是因为STR是一个数组。这就是为什么你仍然需要迭代变量…

$('#select_multiple').multiselect()
var data="cheese,mushrooms";
var dataarray=data.split(",");
$("#select_multiple").val(dataarray);
$("#select_multiple").multiselect("refresh");   
function check_value(){
var str = $('#select_multiple').val();  
const abc = []
str.forEach((s) => {
abc.push(s.replace("cheese", "jam"));   
})


console.log(abc);
}
<!DOCTYPE html>
<html>
<head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/css/bootstrap-multiselect.css" />
</head>
<body>
<select id="select_multiple" multiple="multiple">
<option value="cheese">Cheese</option>
<option value="tomatoes">Tomatoes</option>
<option value="mozarella">Mozzarella</option>
<option value="mushrooms">Mushrooms</option>
<option value="pepperoni">Pepperoni</option>
<option value="onions">Onions</option>
</select>
<button onclick="check_value();">Check Value</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/js/bootstrap-multiselect.min.js"></script>
</body>
</html>

相关内容

  • 没有找到相关文章

最新更新