Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.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:从HTML获取变量名_Javascript - Fatal编程技术网

Javascript:从HTML获取变量名

Javascript:从HTML获取变量名,javascript,Javascript,我试图创建一个可重用的javascript/jQuery函数,其中只有HTML是可编辑的。基本上,jQuery脚本将在表单选择元素上查找某个类,并填充选择框。它将使用一个xml文档,它将被输入JavaScript变量 <form> <select name="topliclist" class="mhs-events" data-event-xml="/xml/events.xml" data-event-text="$fullDate, at ,$startTime,

我试图创建一个可重用的javascript/jQuery函数,其中只有HTML是可编辑的。基本上,jQuery脚本将在表单选择元素上查找某个类,并填充选择框。它将使用一个xml文档,它将被输入JavaScript变量

<form>
    <select name="topliclist" class="mhs-events" data-event-xml="/xml/events.xml" data-event-text="$fullDate, at ,$startTime, - ,$location"></select>
</form>
(不用担心从xml中输入变量,我可以做到。)

“string”变量将等于数据事件文本:

//js
var string = '$fullDate, at ,$startTime, - ,$location';
$.each(string.split(","), function(index, item) {

            //remove $, only put this to specify that it is a variable
            if(item.indexOf('$') == 0) {
                item = $(item.substring(1));
                item = item.toString();
            }

            newString += item
        });

        $(this).append('<option>' + newString + '</option>');
//js
变量字符串=“$fullDate,at,$startTime,-$location”;
$.each(string.split(“,”),函数(索引,项){
//删除$,仅将其设置为指定它是一个变量
if(item.indexOf(“$”)==0){
item=$(item.substring(1));
item=item.toString();
}
新闻字符串+=项目
});
$(this.append(“”+newString+“”);
它不是“10月7日星期一下午6:30-办公楼1”,而是在[object object]-[object object]处填充“[object object]

从数据事件文本属性获取变量名并实际使用它获取JavaScript中的变量值的最佳方法是什么?

更改

item = $(item.substring(1)); // this converts your string to object
item = item.toString();

您不需要将所有项目都添加到newString中,newString将作为一个选项返回所有项目

尝试:

$.each(string.split(“,”),函数(索引,项){
//删除$,仅将其设置为指定它是一个变量
if(item.indexOf(“$”)==0){
item=item.子字符串(1);
item=eval(item).toString();
}
新闻字符串+=项目;
});
$('.mhs事件').append(''+newString+'');

您可以编写如下方法

    function readString(el1, el2, el3, x) {

        var newStr = '';
        var temp = x.split(' ');
        for (var i = 0; i < temp.length; i++) {
            var str = temp[i];
            if (str.indexOf('$') == 0) {
                newStr += eval(str.substring(1, str.length));
            } else {
                newStr += str;
            }

            newStr += ' ';
        }

        return newStr;
    }

做一个console.log并检查你在
newString中得到了什么
我在console.log中得到了同样的东西。试试Chrome中的console.dir()。。。您可以展开对象以查看其属性,这将告诉您要使用哪个属性。我对其进行了转换,因为如果没有,它将返回“fullDate”,而不是fullDate变量的值。想法?另外,我不想为字符串中的每个单词创建新选项。请查看我的评论。我不想为每个单词添加一个新单词。这就是为什么我把它放在我的美元之外。每个报表。就是这样!啊!非常感谢。
item = item.substring(1);
item = eval(item).toString();
$.each(string.split(","), function(index, item) {
            //remove $, only put this to specify that it is a variable
            if(item.indexOf('$') == 0) {
                item = item.substring(1);
                item = eval(item).toString();
            }
           newString += item;
});
$('.mhs-events').append('<option>' + newString + '</option>');
    function readString(el1, el2, el3, x) {

        var newStr = '';
        var temp = x.split(' ');
        for (var i = 0; i < temp.length; i++) {
            var str = temp[i];
            if (str.indexOf('$') == 0) {
                newStr += eval(str.substring(1, str.length));
            } else {
                newStr += str;
            }

            newStr += ' ';
        }

        return newStr;
    }
readString("I", "am" ,"saying", "$el1 $el2 $el3 hello!!!")