首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当一个选项被选中两次时,jQuery显示消息

当一个选项被选中两次时,jQuery显示消息
EN

Stack Overflow用户
提问于 2013-12-05 05:56:37
回答 4查看 1.3K关注 0票数 4

我的表单有一个简单的输入文本,另一个选项选择dropdown.So,我的标记如下所示

代码语言:javascript
复制
 <div id="form-wrap" style="width:500px; margin: 0 auto;">
    <table>
      <tr>
        <th width="55%">Service Name</th>
        <th width="35%">From Price</th>
        <th width="10%"></th>
      </tr>
          <tr id="template">
            <td id="example" width="55%">
              <select name="service-name" id="service-name" style="width:230px;">
                <option value="" selected>--select--</option>
                <option value="service-1">service-1</option>
                <option value="service-2">service-2</option>
                <option value="service-3">service-3</option>
                <option value="service-4">service-4</option>
              </select>
            </td>
            <td width="45%"><input type="text" name="from-price" id="from-price" /></td>
            <td width="10%"><input type="button" id="remove" value="remove row" /></td>
          </tr>
    </table>
    <input type="button" value="+ Add Row" id="add-row" />
  </div>

在表单下面,我有一个名为Add row的按钮。因此,当有人单击它,它将添加另一行和另一个按钮作为删除,这将删除与选定的按钮行行。jQuery代码如下所示

代码语言:javascript
复制
<script type="text/javascript">
  jQuery(document).ready(function() {
    id = 0;
    var template = jQuery('#template');
    jQuery('#add-row').click(function() {
      var row = jQuery(template).clone();
      var templateSelectedIndex = template.find("select")[0].selectedIndex;
      template.find('input:text').val("");
      row.attr('id','row_'+(++id));
      row.find('#remove').show();
      row.find("select")[0].selectedIndex = templateSelectedIndex;
      template.after(row);
    });
    jQuery('#form-wrap').on('click','#remove',function() {
      jQuery(this).closest('tr').remove();
    });

});

在这里工作很好。这是工作中的小提琴链接

http://jsfiddle.net/NewUserFiddle/LT7gM/

但在这件事上我想要另一个额外的选择。你可以看到我有选择

代码语言:javascript
复制
 <select name="service-name" id="service-name" style="width:230px;">
  <option value="" selected>--select--</option>
  <option value="service-1">service-1</option>
  <option value="service-2">service-2</option>
  <option value="service-3">service-3</option>
  <option value="service-4">service-4</option>
</select>

因此,我希望,当某人在行中选择一个选项时,(lets say Service1),然后添加另一个行,然后再次选择相同的前面选择的选项(here it is Service1 as Service1 has been selected in previous row),那么它应该显示一个警告消息,表示对不起,选项名之前已经被选中了。有人能告诉我怎么做吗?任何帮助都将是非常可观的。谢谢。有什么帮助吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-12-05 06:11:19

尝试:

代码语言:javascript
复制
jQuery('#add-row').click(function () {
    var row = jQuery(template).clone();
    var templateSelectedIndex = template.find("select")[0].selectedIndex;
    template.find('input:text').val("");
    row.attr('id', 'row_' + (++id));
    row.find('#remove').show();
    row.find("select")[0].selectedIndex = templateSelectedIndex;
    template.after(row);
    $("#template").find("select:first option:first").prop("selected","selected");
});

这里的演示。

票数 1
EN

Stack Overflow用户

发布于 2013-12-05 06:07:19

作为一个概念。

代码语言:javascript
复制
if $( select#service_1 ) exists

    alert("already added")

else

    add new select box

end if

您似乎很好地掌握了jquery来实现伪代码。

票数 1
EN

Stack Overflow用户

发布于 2013-12-05 06:26:26

如果我错了,请纠正我,但似乎您希望确保用户只能选择每个选项中的一个。换句话说,您希望防止用户在多行中选择服务-1。

为此,您可以将事件侦听器附加到每个select,循环遍历所有select输入,并确保没有找到重复的输入。这是我整理的代码:

代码语言:javascript
复制
jQuery('#form-wrap').on('change', 'select',function(event) {
    var services = {};
    var valid = true;
    $('#form-wrap select').each(function(index, element) {
        if (services[element.selectedIndex])
            valid = false;

        if (element.selectedIndex != 0)
            services[element.selectedIndex] = true;
    });

    if (!valid)
    {
        alert('Sorry, that service has already been selected');
        this.selectedIndex = 0;
    }
});

这是一个工作小提琴

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20392551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档