如何在JS中插入一个var

时间:2016-07-15 04:06:28

标签: javascript jquery

我想插入var" id"在哪里" SP"位于。有可能吗?

var id = $(this).val();
    $("#select2").children("option[value^='SP_']").show();

以上整个代码:

(我尝试这样做是因为我要添加另一个需要_1,_2,_3过滤器的选择(#select3),我将在选项值上使用。)

<select name="select1" id="select1">
  <option value="vazio">Selecione um Estado</option>
  <option value="SP">São Paulo</option>
  <option value="PE">Pernambuco</option>
  <option value="GO">Goiás</option>
  <option value="BA">Bahia</option>
</select>

<select name="select2" id="select2">
  <option value="vazio">Selecione uma Cidade</option>
  <option value="SP_1">Cidade São Paulo</option>
  <option value="SP_2">Cidade São Paulo</option>
  <option value="SP_3">Cidade São Paulo</option>
  <option value="PE_1">Cidade Pernambuco</option>
  <option value="PE_2">Cidade Pernambuco</option>
  <option value="PE_3">Cidade Pernambuco</option>
  <option value="GO_1">Cidade Goiás</option>
  <option value="GO_2">Cidade Goiás</option>
  <option value="BA_1">Cidade Bahia<option>
</select>

<script>
$(document).ready(function() {
    $("#select2").children("option").hide();
});
$("#select1").change(function() {
    var id = $(this).val();
    $("#select2").children("option").hide();
    $("#select2").children("option[value^='SP_']").show();
});
</script>

2 个答案:

答案 0 :(得分:0)

看看它是否有效

Plunker

lein clean

<强>结果

<!DOCTYPE html>
<html>
<body>
  <select id='mySelectBox'>
    <option value='SP_1'>test Val</option>
    <option value='UG_1'>test Val</option>
    <option value='SP_2'>test Val</option>
    <option value='UG_1'>test Val</option>
    <option value='SP_3'>test Val</option>
  </select>
<script>
  var myOpts = document.getElementById('mySelectBox').options;
  for(var i=0; i<myOpts.length; i++) {
    if(myOpts[i].value.indexOf('SP') > -1)
        myOpts[i].setAttribute('id', 'myId_' + i);
  }
</script>
</body>

</html>

答案 1 :(得分:0)

试一试:

<?php
class VarcharDecorator extends Extension {
    function Slugify() {
        return FileNameFilter::create()->filter(trim($this->owner->value);
    }
}