我有下面的jquery和一个返回数组的函数。
我收到的myAnswers = ["Question 1=null", "Question 2=null", "Question 3=yes", "Question 4=yes", "Question 5=yes"]
在下面的for循环中,我想处理每个数组元素的“=”之后的值
if question 1 = null ... do something
elseif question 1 = yes .. do another thing
如何分割这个以便我能够在=
符号后测试值?
$('#Continue').click(function() {
var myAnswers = CheckRadioButtons();
for (i = 0; i < myAnswers.length; i++) {
var splitResult = myAnswers[1].split('=');
var expectedValue = splitResult[1];
}
});
function CheckRadioButtons() {
var $rb = $('input:radio');
var tempArray = new Array();
var questionNo = 0;
for (var i = 0; i < $rb.length; i += 2) {
...some logic
}
return tempArray;
}
答案 0 :(得分:0)
您应该在i
myAnswers[]
for (i = 0; i < myAnswers.length; i++) {
var splitResult = myAnswers[i].split('=');
var expectedValue = splitResult[1];
// Note: Both null and yes will be a string.
if(expectedValue=="null"){ // Do something}
else if(expectedValue=="yes") { // Do something else }
}
答案 1 :(得分:0)
试试这个,value
例如"Question 1=null"
value.split('=')[1] //Right hand side