Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.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 根据列表框选择的数量更新TextArea值_Javascript_Jquery_Listbox_Textarea - Fatal编程技术网

Javascript 根据列表框选择的数量更新TextArea值

Javascript 根据列表框选择的数量更新TextArea值,javascript,jquery,listbox,textarea,Javascript,Jquery,Listbox,Textarea,如果我有一个列表框和一个文本区域: <textarea id="MyTextArea"></textarea> <select id="SelectList" multiple> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="mercedes">Mercedes

如果我有一个列表框和一个文本区域:

<textarea id="MyTextArea"></textarea>

<select id="SelectList" multiple>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>
这只适用于添加字符串一次。但是当我取消选择所有选项时,会出现
0
选项。。我明白了:

无法获取未定义或空引用的属性“indexOf”

在这一行:
var indexOfString=$(“#MyTextArea”).value.indexOf(string)


非常感谢您的帮助。

这个
.value
是一个纯js方法,您不能在jQuery对象
$(“#MyTextArea”)
上调用它,您可以使用
val()
,例如:

$("#MyTextArea").val().indexOf(string);
既然您已经将值存储在变量
txtVal
上,您可以这样调用它:

$("#SelectList").change(function(){
    ...
    else if (count === 0 && txtVal.indexOf(string) != -1) {
        var indexOfString = txtVal.indexOf(string);
        txtVal.substring(0, indexOfString - 1);
    } 
});
注意:您不应该在
textarea
上使用
append()
方法,而应该使用
.val()

而不是:

$("#MyTextArea").append(string);

希望这能有所帮助。

这个
.value
是一个纯js方法,你不能在jQuery对象
$(“#MyTextArea”)
上调用它,你可以使用
val()
,比如:

$("#MyTextArea").val().indexOf(string);
既然您已经将值存储在变量
txtVal
上,您可以这样调用它:

$("#SelectList").change(function(){
    ...
    else if (count === 0 && txtVal.indexOf(string) != -1) {
        var indexOfString = txtVal.indexOf(string);
        txtVal.substring(0, indexOfString - 1);
    } 
});
注意:您不应该在
textarea
上使用
append()
方法,而应该使用
.val()

而不是:

$("#MyTextArea").append(string);
希望这有帮助。

应该是:

var indexOfString = $("#MyTextArea").val().indexOf(string);
因为
$(“#MyTextArea”)
是jQuery元素

它应该是:

var indexOfString = $("#MyTextArea").val().indexOf(string);
因为
$(“#MyTextArea”)
是一个jQuery元素