I have data returned from an ajax call: INSERT_OK_something
.
I would like to use a switch statement with a wildcard like INSERT_OK_*
and pass "something" like a id variable to my url.
switch (data) {
case "ERROR":
$("#alert").dialog( "open" ).html( "Error" );
return false;
case "INSERT_OK_*":
var url = "index.php?op=ok&id=" + something;
window.location = url ;
return false;
}
How would I do this?
I have data returned from an ajax call: INSERT_OK_something
.
I would like to use a switch statement with a wildcard like INSERT_OK_*
and pass "something" like a id variable to my url.
switch (data) {
case "ERROR":
$("#alert").dialog( "open" ).html( "Error" );
return false;
case "INSERT_OK_*":
var url = "index.php?op=ok&id=" + something;
window.location = url ;
return false;
}
How would I do this?
Share Improve this question edited Apr 11, 2013 at 8:03 huysentruitw 28.2k10 gold badges95 silver badges141 bronze badges asked Apr 11, 2013 at 7:19 Paolo RossiPaolo Rossi 2,51012 gold badges47 silver badges70 bronze badges1 Answer
Reset to default 7This little trick will do (see jsFiddle):
var data = "INSERT_OK_BLABLA";
switch (data) {
case "INSERT_OK_" + data.slice("INSERT_OK_".length): // emulate INSERT_OK_*
var url = "index.php?op=ok&id=" + data.slice("INSERT_OK_".length);
alert(url);
break;
default:
alert("default");
break;
}
or using startsWith
(see jsFiddle):
switch (true) {
case data.startsWith("INSERT_OK_"):
var url = "index.php?op=ok&id=" + data.slice("INSERT_OK_".length);
alert(url);
break;
default:
alert("default");
break;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745137218a4613260.html
评论列表(0条)