2017-07-28 4 views
-3

:私はA国のリストを持っているとして、国の残りの部分と一緒に、配列にそれをプッシュする前に、文字列から文字列を取り除いて配列に挿入する方法は?私は次のことを実行しています

var countries = []; 
$("#usp-custom-3 option").each(function() { 
    var single = $(this).text(); 
    if($(single == "United States of America")) { 
    $(this).text() == "United States"; 
    } 
    countries.push(single); 
    console.log(single); 
}); 

基本的に私が何をしようとしています何がUnited StatesUnited States of Americaを変換することですselect option

+0

多分 'single ==" United States ";'? singleは配列に渡すオブジェクトですから –

+0

あなたは 'single'に代入していないので' $(this).text() 'を単独で押します。 – Durga

+0

なぜdownvoteですか? –

答えて

2
var countries = []; 

    $("#usp-custom-3 option").each(function() { 
    var single = $(this).text(); 
    if(single == "United States of America") { 
    single= "United States"; 
    } 
    countries.push(single); 
    console.log(single); 
    }); 
+0

すばらしいおかげさまで –

0
var countries = []; 
$("#usp-custom-3 option").each(function() { 
    var single = $(this).text(); 
    if (single == "United States of America") { 
     single = "United States"; 
    } 
    countries.push(single); 
    console.log(single); 
}); 

これを試してください。

+0

Of America https:// jsfiddleが表示されます。 net/pz3ce0o6/172/ –

+0

'=='は代入演算子ではありません。 –

0

問題を修正し、少しリファクタリングを追加しました。

var countries = []; 
var us = 'United States'; 
var usa = 'United States of America'; 

$("#usp-custom-3 option").each(function() { 
    var single = $(this); //gets jquery instance of the element 
    if(single.text() == usa) { //Check if its text is USA 
    single.text(us); //Replace the option text with 'United States' 
    } 
    countries.push(single.text()); //Push value into country list 
}); 
+1

いつも 'ua' – Durga

+0

チェックコンソールを押しますhttps://jsfiddle.net/pz3ce0o6/174/ –

+0

@Durga私はちょうどそれも気づきました。 –

1

この

var countries = []; 
 
$("#usp-custom-3 option").each(function() { 
 
    var single = $(this).text(); 
 
    if (single == "United States of America") { 
 
     single = "United States"; 
 
    } 
 
    countries.push(single); 
 
    console.log(single); 
 
}); 
 

 
console.log(countries);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select id="usp-custom-3"> 
 
    <option>United States of America</option> 
 
    <option>India</option> 
 
    <option>UK</option> 
 
    <option>United States of America</option> 
 
    <option>India</option> 
 
    <option>UK</option> 
 
    <option>United States of America</option> 
 
</select>

0

を試してみて、それだけでjqueryのを使用するためにjQueryを使ってコードを混合することが、とても重要である理由私はいつも疑問に思います。

それは(ノードを取得し、1つの簡単なチェックでフラットな配列にマッピングし)そのような単純なものでした:

var nodesArray = [].slice.call(document.querySelectorAll("#some_select option")).map(function(v){return (v.innerText == 'United States of America') ? 'United States' : v.innerText}); 
console.log(nodesArray.toString()); 
0

$(この)は.text()==「米国」;問題のある行です。

"=="は比較式であり、代入式ではありません。

これに値を割り当てるには$(this).text( "任意のテキストが必要です")

関連する問題