选择循环中选择的选项

时间:2016-03-12 11:45:03

标签: php jquery forms

我正试图从选择框中获取值。表单在我的页面上多次放置一个php循环(这就是我使用类而不是id的原因)。

    <html>
    <body>
    <table>
        <form class="lesmaker">
            <tr>
                <td colspan="5">
                    <select name="type" class="type">
                        <option value="open">Open vraag</option>
                        <option value="info">Informatie</option>
                        <option value="discussie">Klassikaal</option>
                        <option value="meerkeuze" selected="selected">Meerkeuze</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="5">
                    <select name="media" class="media">
                    <?php foreach($images as $image){if(substr($image,0,1) != '.'){ ?>
                    <option style="background-image:url(<?=$d_images.$image?>)" value="<?=$image?>" <?php if($r_media== $image) echo 'selected="selected"'; ?>><?=$image?></option>
                    <?php }} ?>
                </select>
                </td>
            </tr>
        </form>
    </table>
    <table>
        <form class="lesmaker">
            <tr>
                <td colspan="5">
                    <select name="type" class="type">
                        <option value="open">Open vraag</option>
                        <option value="info">Informatie</option>
                        <option value="discussie">Klassikaal</option>
                        <option value="meerkeuze" selected="selected">Meerkeuze</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="5">
                    <select name="media" class="media">
                    <?php foreach($images as $image){if(substr($image,0,1) != '.'){ ?>
                    <option style="background-image:url(<?=$d_images.$image?>)" value="<?=$image?>" <?php if($r_media== $image) echo 'selected="selected"'; ?>><?=$image?></option>
                    <?php }} ?>
                </select>
                </td>
            </tr>
        </form>
    </table>
</body>
</html>

使用jQuery我试图获得所选的选项。

    $(function(){
        $('.lesmaker').each(function() {                
            console.log($(this).find('.type').val());
        });
    });

上面的代码不起作用。我该怎么做呢?

2 个答案:

答案 0 :(得分:1)

findViewById(R.id.list_item).setSelected(true);

您需要将HTML更改为以下内容:

$(function() {
  $('.lesmaker').each(function() {
    console.log($(this).find('table tr td').find('.type').val());
  });
});

表格需要在表格之外或在td之内:)

答案 1 :(得分:0)

&#13;
&#13;
$(function() {
  $('.type').each(function() {
    console.log($('option:selected', this).val());
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
  <form class="lesmaker">
    <tr>
      <td colspan="5">
        <select name="type" class="type">
          <option value="open">Open vraag</option>
          <option value="info">Informatie</option>
          <option value="discussie">Klassikaal</option>
          <option value="meerkeuze" selected="selected">Meerkeuze</option>
        </select>
      </td>
    </tr>
    <tr>
      <td colspan="5">
        <select name="type" class="type">
          <option value="open">Open vraag</option>
          <option value="info">Informatie</option>
          <option value="discussie" selected="selected">Klassikaal</option>
          <option value="meerkeuze" >Meerkeuze</option>
        </select>
      </td>
    </tr>
    <tr>
      <td colspan="5">
        <select name="type" class="type">
          <option value="open">Open vraag</option>
          <option value="info" selected="selected">Informatie</option>
          <option value="discussie" >Klassikaal</option>
          <option value="meerkeuze" >Meerkeuze</option>
        </select>
      </td>
    </tr>
  </form>
</table>
&#13;
&#13;
&#13;

只需使用select类并使用$('option:selected', this).val()