简体   繁体   中英

How To pass Variable as an String in JavaScript

I want to pass Var Variable value in Javascript function.I need to pass "name" in the field of "Raju" name must be passed as an string value.How i will do it.

My code is:

function setSelectedIndex(s, v) {
    for (var i = 0; i < s.options.length; i++) {
        if (s.options[i].text == v) {
            s.options[i].selected = true;
            return;
        }
    }
}
var name = '@ViewData["CustName"]';
setSelectedIndex(document.getElementById('Cust_Id'), "Raju");

You can't.

But you can hash it like this:

var name = "Raju"     
var your_Var = { name: "Raju", value: whatever_you_wish}

than you can pass it like this

 setSelectedIndex(document.getElementById('Cust_Id'), your_Var[name]);

try this,

if (s.options[i].text == v.toString()) {
}

您只需要传递您的var名称即可。

setSelectedIndex(document.getElementById('Cust_Id'), name);

I pass Variable value in the field of "Raju" by '' + name + ''...

It is my old Code

> var name = '@ViewData["CustName"]';
> setSelectedIndex(document.getElementById('Cust_Id'), "Raju");

It is my Solution Code,Now i got selected names in dropdownlist..

> var name = '@ViewData["CustName"]';
> setSelectedIndex(document.getElementById('Cust_Id'), '' + name + '');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM